Would you please help me to figure out the result? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Would you please help me to figure out the result?

/* Q. 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. Use a loop to iterate through the array and increase all items. I tried to figure out the result according to my knowledge on Javascript. Thank you. */ var prices = [98.99, 15.2, 20, 1026]; //your code goes here prices[0] = 98.99; prices[1] = 15.2; prices[2] = 20; prices[3] = 1026; console.log(prices[i]); function newPrices(prices,increase){ this.prices = prices; this.increase = increase; this.newPrice = function(){ this.price = sum(prices, increase); } /* var i=0; do{ return increase; i++; } while(i<=i.length); */ var x = newPrices(prices, increase); var x.price = }

5th Dec 2022, 8:09 AM
Sony
Sony - avatar
10 Answers
+ 6
You commented out the loop and you need it for all prices. You have a "sum" but don't have anything saying what that should do.
5th Dec 2022, 8:12 AM
Ausgrindtube
Ausgrindtube - avatar
+ 5
Let me ask you a few questions, that hopefully send you in the right direction. How are you going to add the numbers? How do you loop through an array? (Hint: look at that lesson) Can you add numbers in a loop?
5th Dec 2022, 8:29 AM
Ausgrindtube
Ausgrindtube - avatar
+ 3
First you have array, then you manually take values with index, imagine you have 1000 items in array will you do the same? Then you log console.log(prices[i]) where you define i variable? i is undefined Then you make object constructor function, not regular function, and inside it you set values and one method, then you call this constructor function(used to create objects) inside itself with wrong syntax, syntax we use for regular function what can lead to many bugs if used in this way. Then in unfinished line you try to do something with value of x.price, you will have x.prices if you look at line: this.prices = prices But as I said you are creating new object from constructor inside constructor body and try to access it, this is very wrong, if you need to use object constructors for this task please read lections again, you didn't understand how to use it. Here are amazing resource about it: https://www.theodinproject.com/lessons/node-path-javascript-objects-and-object-constructors
5th Dec 2022, 8:58 AM
PanicS
PanicS - avatar
+ 2
To fix your code we will need to solve problem from beggining,and share with you completed code, this won't help you, follow resource i posted it dive very deep in explaining constructors. Also this can be solved without constructors, you just need regular function what accept given array, and increase amount, then inside function you return new changed array. You can use any loop(for, while) or array methods like forEach, then add this increase amount to price.
5th Dec 2022, 9:03 AM
PanicS
PanicS - avatar
+ 2
Just add newPrices.push(prices[i]) Inside loop. If you wanna learn please read documentation about this topic before moving on to next lections. Every project here on sololearn are about things we learned before, and it is test of your understanding.
6th Dec 2022, 4:35 PM
PanicS
PanicS - avatar
+ 1
If you are doing sololearn project, your goal inside it is not to use object constructors. Goal is to use array methods, you learned in lections before Startup code look something like this: function main() { var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; var newPrices = []; //your code goes here // in tip you got information to use loop // you can use any, loop for/while, but for is better in this case because we dont need to create variable outside of loop for(let i = 0; i < prices.length; i++) { // how can you add new item to array? What method we use, use this method and you are solved problem } // then you return or log result, i used console log console.log(newPrices); } check this to find method you need to use: https://www.w3schools.com/js/js_array_methods.asp
5th Dec 2022, 6:35 PM
PanicS
PanicS - avatar
0
That's what I was trying.. but next part of the code what is going to be.. I'm unable to figure out.. that's why I'm asking to....
5th Dec 2022, 8:15 AM
Sony
Sony - avatar
0
Thank you for your supports .. but problem is how to increase the prices? What's the way to execute in codes? Would You please? PanicS
5th Dec 2022, 5:43 PM
Sony
Sony - avatar
0
Still trying to .. var increase = 9; var prices = [98.99, 15.2, 20, 1026]; function newPrice(prices, increase){ return prices + increase; } var a = newPrice(prices, increase); a.prices = var = 0; do{ console.log(a.prices); prices++; } while(prices<=prices.length); newPrice();
5th Dec 2022, 6:25 PM
Sony
Sony - avatar
0
No reflection... so disappointed!
6th Dec 2022, 2:31 PM
Sony
Sony - avatar