Following are the code to the given question:
function organizeItems(items) //defining a method organizeItems that takes an items in parameter
{
var result = {};//defining an array as a result
for(item of items) //defining for loop to count items value
{
if(!(item.category in result))// use if to check category exists in dictionary
{
result[item.category] = []; //use result to assign value in blank list }
var name = item.itemName; //defining variable that holds itemName value
if(item.onSale)//use if to check item value
{
name += '($)';//use name to add string value
}
result[item.category].push(name); //use result to hold value
}
return result;//return result value
}
Code explanation:
Defining a function "organizeItems" that takes "items" variable in parameter.Inside the function, an array "result" is declared, and a for loop is defined that counts items values and use if block to check categories exist in the dictionary.In the next step, a variable "name" is declared that holds "itemName" value, and use if to check item "onSale" it adds string value and uses a result variable to hold value, and define return keyword to return a "result" value.Learn more: