I had a challenge that says :
-
you have an array of these items :
const items = ["gold cup", "puppy", "sword", "whale's tooth", "squid tentacle"]; -
and this array shows items’ price respectively :
const prices = [5, 4, 10, 20, 100]; -
every price of an item isn't mentioned in the items array is equal to 1
create a function that calculate the value "the price " of some chosen items which are located in ChosenItem array. So I used nested for loop and used if and else if statments. it worked fine but it doesnt add 1 for every unmentioned item . and it adds more than 1. here's the code :
let TotalPrice = 0;
let ChosenIems = ["puppy", "sword","eagle eye"];
function PricesCalculator() {
for (i = 0; i < ChosenIems.length; i++) {
for (x = 0; x < items.length; x++) {
if (ChosenIems[i] === items[x]) {
TotalPrice += prices[x];
}
else if (ChosenIems[i] !== items[x]) {
TotalPrice += 1;
}
}
}
return TotalPrice;}
console.log(PricesCalculator(), TotalPrice);
Aucun commentaire:
Enregistrer un commentaire