JavaScript store manager code coach | Sololearn: Learn to code for FREE!

0

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); } }

12/10/2020 7:39:35 AM

Sudo

28 Answers

New Answer

+35

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) }

+11

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) }

+8

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.

+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(', ')+' ]'); }

+2

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.

+1

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) ; }

+1

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) }

+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.

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); }

0

Do this, they say "make a loop", we just have to make a new array adding the increase to the current price while the new price array is the same as the previous one. function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //tu código va aquí. = your code goes here. do { var newprices = [98.99 + increase, 15.2 + increase, 20 + increase, 1026 + increase]; } while (newprices === prices); console.log(newprices); }

0

function main() { var i = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here var newPrices = [98.99 + i, 15.2 + i, 20 + i, 1026 + i]; console.log(newPrices); }

0

//for loop all the element+increase for (i=0; i<prices.length; i++){ prices[i] = increase + prices[i]; } console.log(prices)

0

is a loop necessary? what's the difference between using loop and no loop?

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) }

0

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); } }

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)

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) ; }

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) }

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) }

0

My work for(i=0;i<prices.lengh;i++){ Console.log(prices[i] + increase) }