How to get the outputted results into a single array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to get the outputted results into a single array

I am trying to complete the store manager challenge and I am close but struggling to put the final results into a single array. This is my code: function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for (element of prices) { newPrice = element + increase; console.log(newPrice) } } How can I get it to console into a single array rather than a list that it is currently doing at the moment?

12th Jan 2021, 8:09 PM
Zach Best
Zach Best - avatar
7 Answers
+ 1
Log to console after the loop, not inside the loop. for( let i = 0; i < prices.length; i++ ) { prices[ i ] += increase; } console.log( prices );
13th Jan 2021, 3:42 PM
Ipang
+ 2
Don't worry, we all stumble on something as we learn, it's just a part of the process 👌
13th Jan 2021, 3:52 PM
Ipang
+ 1
Zach Best Push the elements in an array var arr = []; for (element in prices) { arr.push(newPrice); }
12th Jan 2021, 8:15 PM
A͢J
A͢J - avatar
+ 1
Ahh Thank you. I should of thought of that, it seems so obvious now but hey I get it now. Thanks a lot. I will hopefully remember this lesson for future cases.
13th Jan 2021, 3:47 PM
Zach Best
Zach Best - avatar
0
ahh thanks. i should of thought of that, but now it is putting each element into its own separate array: I have changed my code to use a for loop (namely as I wanted to try using one) for (i = 0; i < 4; i++) { let arr = []; let result = prices[i] + increase; arr.push(result); console.log(arr); } } I am now getting the following results: [ 107.99 ] [ 24.2 ] [ 29 ] [ 1035 ] when I was expecting to get a single array like this: [ 107.99, 24.2, 29, 1035 ]
12th Jan 2021, 9:11 PM
Zach Best
Zach Best - avatar
0
When it comes to challenges, note carefully the task description. Be sure to check whether the task requires you to save <newPrice> to another array or to update <prices> directly. Is <increase> a value or percentage?
13th Jan 2021, 2:53 AM
Ipang
0
Right I am close but still no cigar as the saying goes. I have managed to get the elements into a single array, however it console.log each iteration of prices array, when I need it to print the final iteration only. [ 107.99, 15.2, 20, 1026 ] [ 107.99, 24.2, 20, 1026 ] [ 107.99, 24.2, 29, 1026 ] [ 107.99, 24.2, 29, 1035 ] My code: 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); } } I thought console.log(prices[3]) would achieve it but it just prints the fourth element of each array from above like so: 1026 1026 1026 1035 how can I get it to print just the entire array of the fourth iteration?
13th Jan 2021, 3:34 PM
Zach Best
Zach Best - avatar