+ 1
44 Code Project: Store Manager
You are working on a Store Manager program, which stores the prices in an array. You need to add functionality to increase the prices by the given amount. The increase variable is taken from user input. You need to increase all the prices in the given array by that amount and output to the console the resulting array. // Help, please. My code outputs an array without adding the number that’s input. https://code.sololearn.com/c27NnRbZkKSf/?ref=app
8 Antworten
+ 3
Cristian Gabriel Mazzulla
Rupali
Thank you for this guidance!
I followed the advice one step at a time. I used this:
var i;
for (i=0; i<prices.length; i++){
prices[i]= (prices[i]+ increase);
}
console.log(prices);
// for the 🎖
+ 3
Rupali
I always wondered why the code playground never indicates when your program runs correctly. It always just says No Output.
+ 2
First, in that code you share, there was a missing } at the end
Second, the for condition doesn't make sense, you must compare i against the number of elements in your array
Third, you are not accessing the array elements, you must access each of them to modify them
https://www.sololearn.com/Course/JavaScript/1239/?ref=app
https://www.sololearn.com/Course/JavaScript/1241/?ref=app
+ 2
Cristian Gabriel Mazzulla I called main() bcz when you run code in sololearn's playground it won't get executed, but when you're submitting in code coach then no need of calling main(). I used new array for simplicity sake...And the last I don't know.
+ 2
Rupali you're right, in the playground you have to call it.
Why do you need to call it there but not when you submit it??🤨
+ 1
function main() {
var increase = parseInt(readLine(), 10);
var prices = [98.99, 15.2, 20, 1026];
//your code goes here
var result = [];
for (let i=0; i<prices.length; i++){
result[i] = prices[i] + increase;
}
console.log(result);
}
main()
+ 1
Rupali You don't need to call main, you don't need a new array, and no one needs the answer but the explanation.
+ 1
Cristian Gabriel Mazzulla code coach calls main() by its own but if you change function name main() to something else you have to call this manually else it won't work. 😅😅