+ 1

JavaScript store manager code coach

Please how do I solve this code coach problem on JavaScript I have tried this it works but the results is saved in a single array please is there a way I can combine all results in one array? What I did: function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var i; for(i=0;i<4;i++){ var n = [prices[i] + increase]; //var result = new Array(n); console.log(n); } }

10th Dec 2020, 7:39 AM
Sudo
Sudo - avatar
38 Réponses
+ 41
This is how I did it. First I created empty array. Then I looped through prices array and in every loop I used array push method to add increased number to the new array. I think it is better to put prices.length than a fixed number (in this case 4) in a loop. Since we are all mostly beginners the most important thing is that it solves the task, but we should try to make our code as conventional and reusable as possible. function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var newPrices = []; for( i = 0; i < prices.length; i++){ newPrices.push(prices[i] + increase); } console.log(newPrices) }
16th Dec 2020, 12:28 PM
Nemanja Kostovski
Nemanja Kostovski - avatar
+ 15
Easiest way to solve this code challenge....... function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for (i=0; i<4; i++){ prices[i] = increase + prices[i]; } console.log(prices) }
11th Jan 2021, 5:04 PM
Taral Patel
Taral Patel - avatar
+ 9
I would like to add that it is better to put length as a separate variable, then put it in the loop. This is mainly for performance, since loop will check every time the length of the array. If we premake that number as a variable, we save some processing time.
17th Dec 2020, 1:29 PM
Nemanja Kostovski
Nemanja Kostovski - avatar
+ 5
I basically created an empty array, i used for each to loop through every value while also saving it in the new array with the increment. trying to keep as short and simple as possible. Like my professor says. "KEEP IT SIMPLE STUPID" lol function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var array=[]; prices.forEach(function(item) { array.push(item+increase); }) console.log('[ '+array.join(', ')+' ]'); }
31st Dec 2020, 10:16 PM
Jorge Guerrero
Jorge Guerrero  - avatar
+ 3
kimmy For example, this practise is called store manager. In this case there are only 4 prices/items, but I think most of the stores have a lot more than 4 items. To answer your questions: no, loops aren't necessary, you can hardcode everything, but imagine hardcoding 2000 different store items and their prices, and then few days later there is a sales action with 10% off on prices, and than you have to manually change all the prices. Loops are there to do that for you and make your life a lot easier.
9th Apr 2021, 4:41 AM
Nemanja Kostovski
Nemanja Kostovski - avatar
+ 2
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var n = []; for( i = 0; i < prices.length; i++){ n[i]=(prices[i] + increase); } console.log(n) ; }
7th Jan 2021, 6:06 AM
Rupan Dutta
Rupan Dutta - avatar
+ 2
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for (i=0; i< prices.length; i++){ prices[i] = increase + prices[i]; } console.log(prices) }
24th Jun 2021, 12:01 AM
Md.Nakibul Islam Hridoy
Md.Nakibul Islam Hridoy - avatar
+ 2
Here is how I found it function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for(i=0;i<prices.length;i++);{ prices[0]+=increase; prices[1]+=increase; prices[2]+=increase; prices[3]+=increase; console.log(prices); } }
27th Jul 2021, 9:47 PM
Joseph Lungu
Joseph Lungu - avatar
+ 1
Simpler way to do it. function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var newArr = []; prices.forEach( (item)=>{ newArr.push(item + increase); }) console.log(newArr); } forEach loop does not change values of an array, so you create a new empty array and use the push method to add items in it after looping through the prices array.
10th Sep 2021, 3:37 AM
Martian
Martian - avatar
+ 1
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here count=0; while (count<prices.length) { prices[count] = prices[count] + increase; count++; } console.log(prices); }
4th Jan 2023, 5:54 AM
Ogün Orta
Ogün Orta - avatar
0
This worked for me: function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; for (i = 0; i < 4; i++) { prices[i] += increase; } console.log(prices); }
14th Jan 2021, 11:31 PM
Elgin
Elgin - avatar
0
//for loop all the element+increase for (i=0; i<prices.length; i++){ prices[i] = increase + prices[i]; } console.log(prices)
15th Mar 2021, 11:37 PM
Nick GE
Nick GE - avatar
0
is a loop necessary? what's the difference between using loop and no loop?
8th Apr 2021, 10:37 PM
kimmy
0
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; for (i=0; i<4; i++){ prices[i] = increase + prices[i]; } console.log(prices) }
12th May 2021, 3:00 AM
Md. Rakibul Islam
Md. Rakibul Islam - avatar
0
I used while loop to do this, also with help from everyone here :) //your code goes here var i = 0; while (i < 4){ prices[i] =prices[i]+ increase i++ } console.log(prices)
21st Sep 2021, 4:57 AM
Sadia Afrin
Sadia Afrin - avatar
0
function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026] ; for(i=0; i< prices.length ; i++) { prices[i] = increase + prices [i] ; } console.log(prices) ; }
1st Oct 2021, 8:13 AM
Vijayalakshmi K
0
Hello everyone, This is how i did it. The result is correct but i 've seen here that you have done it differently. I guess its like maths :) function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for (i=0;i<4;i++){ prices[i]+=increase; } console.log(prices) }
3rd Nov 2021, 1:28 PM
mstavra
mstavra - avatar
0
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++){ prices[i] = prices[i] + increase } console.log(prices) }
18th Nov 2021, 9:21 PM
Mohammad Amaierh
0
My work for(i=0;i<prices.lengh;i++){ Console.log(prices[i] + increase) }
22nd Nov 2021, 4:16 PM
Khalid Albasis
Khalid Albasis - avatar
0
Best way to solve this , more understandable function main() { var w= parseInt(readLine(), 10); var a= [98.99, 15.2, 20, 1026]; //your code goes here var b = a[0]+w var c = a[1]+w var d = a[2]+w var e = a[3]+w console.log(new Array(b,c,d,e)); }
30th Nov 2021, 4:24 PM
Kamal Adewale