0

Pls help me in this question how do i get to increase the prices on the array.

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.

21st Jan 2022, 9:52 PM
Okere Samuel
Okere Samuel - avatar
7 Answers
+ 2
You need to increase each price from array, so in your for loop you can do console.log(prices[i] + increase) // 98.99 + input, for first index and so on you need to take value from price array, but you loging index and [0] - this [0] will probably give some error in console you can also use map method - es6 prices.map((price) => { console.log(price + increase); }) much cleaner than for loop Here is more about map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
22nd Jan 2022, 1:09 AM
PanicS
PanicS - avatar
+ 1
What you mean by doubles? Map is very similar as forEach, you can see diference here, also you can see how both are used, what is faster.... https://codeburst.io/javascript-map-vs-foreach-f38111822c0f
22nd Jan 2022, 7:47 PM
PanicS
PanicS - avatar
+ 1
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here let newPrice = []; for ( i = 0; i < prices.length; i++ ) { let priceIncrease = prices[i] + increase; newPrice.push(priceIncrease); } console.log(newPrice); } I searched online, put pieces of code together and got this, it worked for me.
16th Apr 2022, 12:04 AM
Luis
0
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here } //This is the code
21st Jan 2022, 9:56 PM
Okere Samuel
Okere Samuel - avatar
0
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for (let i = 0; i < prices.length; i++){ console.log(i, [0]) } } //this is my own solving pls tell me where am not right thanks
21st Jan 2022, 10:01 PM
Okere Samuel
Okere Samuel - avatar
0
Thanks alot!
22nd Jan 2022, 8:55 AM
Okere Samuel
Okere Samuel - avatar
0
Ok the map method prints out in doubles while forEach prints singles just like the for loop. i think?
22nd Jan 2022, 9:04 AM
Okere Samuel
Okere Samuel - avatar