Ask coding questions

← Back to all posts
How do I complete this simple route function
nanasv (0)

Basically when I log the first function its work fine display 92...but when I connect it with the second function it display error Rep isn't iterable.
"`"

//Array of integers for searching maximum integer
const mixItems = [2,6,96,35,6];

//object
const itemsObj = {
"Biscuits": 12,
"Bread": 94,
"Danon_milk": 16,
"Golden_morn": 50
};

//Helper function to find the maximum integer

function eventHandler(par){
let maxValSearch = [];
maxValSearch = [...par];

var maxVal = par.reduce(function(initEl,finEl){
maxValSearch = ([initEl,finEl]);
return Math.max.apply(null,maxValSearch);
});
return maxVal;
}
eventHandler(mixItems);
//other function
const itemsFunction = function(obj,prop){

totalSellParItem = " ";
for(let i in obj){
if(obj.hasOwnProperty(i)){
prop = i;
result = obj[prop];
if(result !== undefined){
totalSellParItem = result;
let totalBisPrice = eventHandler();
return totalSellParItem += totalBisPrice;
}else {
return "result of the property not found ";
}
}else {
return "this property is not found ";
}
}
return totalSellParItem;
}
let propPrompt = prompt('enter the property here');
const eventHandlers = itemsFunction(itemsObj,propPrompt)
window.alert(eventHandlers +":"+ 'searching... ');

"`"

Answered by notGilbert (121) [earned 5 cycles]
View Answer
Comments
hotnewtop
notGilbert (121)

You might want to use Object.entries to convert the Object into an iterable array.

example:

const food = {
  apple: 10,
  banana: 15,
  orange: 20
}

Object.entries(food).forEach(v => {
  const key = v[0];
  const val = v[1];
  
  console.log(`${key} heals ${val}hp`);
})
nanasv (0)

@notGilbert
You see I don't seems to have problem with (Object),but array.
Par is the problem,there was nothing to iterate through.when looking carefully I defined the par,and its work.
About your solution its help cause, my object result always hit the first property and return even when different property is provided. but with your method hope to solve it.
I haven't finish, the aim of this is manipulate result, method, properties etc.
Target is to add sell of one item money to the initial money in the account.
Objective =learning how to manipulate js. am always adding one problem to existent ones, am inviting you to always seek solutions with me thanks.
You can see the current code.

//Array of integers for searching maximum integer 
const mixItems = [2,6,96,35,6];

 //object
   const itemsObj = {
  "Biscuits": 12,
  "Bread": 94,
  "Danon_milk": 16,
  "Golden_morn": 50
};



 //Helper function to find the maximum integer 
  
  function eventHandler(par){ 
  let maxValSearch = [];
par = mixItems;
    
 var maxVal = par.reduce(function(initEl,finEl){
 maxValSearch = ([initEl,finEl]);
return Math.max.apply(null,[...maxValSearch]);
 }); 
    return maxVal;
  }
  eventHandler(mixItems);
//other function 
const itemsFunction = function(obj,prop){
  
totalSellParItem = " ";
  for(let i in obj){
   if(obj.hasOwnProperty(i)){
     prop = i;
     result = obj[prop];
     if(result !== undefined){
       totalSellParItem = result;
      let totalBisPrice = eventHandler();
       return totalSellParItem += totalBisPrice;
     }else {
       return "result of the property not found ";
     }
   }else {
     return "this property is not found ";
   }
 }
   return totalSellParItem;
     }
  let propPrompt = prompt('enter the property here');
const eventHandlers = itemsFunction(itemsObj,propPrompt)
window.alert('Total_Item_sells' +":"+ eventHandlers);