+ 2

JAVASCRIPT: Store Manager

In JavaScript course, under Core Objects > 44 Code Project. You need to write a store manager program wherein you need to increase the price (in an array) depending on user's input. So the user's input will increase each price in that array. I could say that I was able to get the logic behind it but the problem is, it is actually different to the expected output. Here's my program below: function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for (var i = 0; i < prices.length; i++) { console.log(prices[i] + increase); } } Sample User Input: 9 My Output: 107.99 24.2 29 1035 Expected Output: [ 107.99, 24.2, 29, 1035 ] I can't seem to display it in a single line.

7th Oct 2021, 8:36 AM
Rolly U. Valdemoro Jr.
Rolly U. Valdemoro Jr. - avatar
9 odpowiedzi
+ 1
Rolly U. Valdemoro Jr. Don't print each value just increase price and finally print array prices for (var p in prices) { prices[p] = prices[p] + increase; } console.log(prices);
7th Oct 2021, 8:49 AM
A͢J
A͢J - avatar
+ 3
Same solution, but using while loop function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var x=0 while (x<prices.length) { prices[x] += increase x++ } console.log(prices) }
6th Jun 2022, 12:42 PM
Anush Avetisyan
+ 2
Hello, this is my solution for this exercise: function main() { var temp; var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; for(i=0;i<prices.length;i++){ temp=prices[i]; temp+=increase; prices[i]=temp; } console.log(prices); }
9th Aug 2022, 2:58 PM
Mohamed Belgazem
Mohamed Belgazem - avatar
+ 1
Rolly U. Valdemoro Jr. Hi Rolly, you need to store the values of incremented price to the price variable & then console log it out of the loop. So, it would be price = price[i] + increase & then console.log price out of the for loop
7th Oct 2021, 8:49 AM
zexu knub
zexu knub - avatar
+ 1
for (x = 0; x < prices.length; x++) { prices[x] += increase; } console.log(prices);
9th Oct 2021, 2:38 PM
Ben
Ben - avatar
+ 1
// here is the simplest answer : // I hope you can understand easily , if you not just let me know function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here const newPrices = prices.map(num => num + increase); console.log(newPrices); }
2nd Jan 2023, 3:24 PM
Ankit Mavani
+ 1
for ( let p = 0; p < prices.length; p++) { prices[p] = prices[p] + increase; } console.log(prices);
9th Feb 2023, 6:55 AM
Luke Olende
0
Thanks for the help, my bad. So I just have to store it in the variable first and the console log should be outside my for loop. I have the correct output now, I appreciate all the help. Regards, Rolly
7th Oct 2021, 9:08 AM
Rolly U. Valdemoro Jr.
Rolly U. Valdemoro Jr. - avatar
0
Hello. Here is my solution. function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var sum = []; for(i = 0; i<prices.length; i++) { sum[i] = (prices[i] + increase); } console.log(sum) }
15th Dec 2021, 8:45 PM
Mircea Teodor Cofa
Mircea Teodor Cofa - avatar