First time posting on here. I am a bit stuck. The assignment is as follows:
Create a function called priceLookup to find the price of a single item. The function should accept the array of items and an item name (string) as parameters, and return the price of the item with that name. If the item cannot be found, return the string "No item found with that name"
** Assume this test data can be other arguments entered into the function ** ( I think I used the correct terminology)
//Test data for the problems
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
]
Here is my code and where I am stuck:
function priceLookup(items, name){
let result = 0;
for (i = 0; i < items.length; i++){
if (name == items[i].itemName){
(result =+ items[i].price)
} else { result = "No item found with that name"
}
}
return result;
}
So I guess my question is, why is my else statement cancelling out the if before it? If I remove that I pass the testing on getting the name called, but fail on if the name doesn't exist that it shows "No item found with that name"
Aucun commentaire:
Enregistrer un commentaire