Array with for loop in javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Array with for loop in javascript

How to iterate throw an array of number in js? Given array of number values called prices var prices= [40,56,67,87,90]; An amount number will be given as user input or a static variable var amount = 12; How to added this amount to each element in the array? Using a for loop with the array.length method as test condition: for(var i= 0;i<prices.length;i++){ //Here goes your code to increase each prices element prices[i] += amount; }

21st Dec 2020, 12:44 PM
HBhZ_C
HBhZ_C - avatar
8 Answers
+ 6
Hi there.. I really appreciate that you wanted to teach others what you've learnt. That's great.... Keep teaching others... 👍👍👍☺☺☺😊😊😊 But, this is a wrong place to teach others. This section is for asking questions and other users will try to answer it and help. 💕💕💕💕 You can post your lesson in your posts section in your profile.😃😃 -> Profile -> Goto Posts -> Create new post by clicking on bottom-right plus icon -> write your lesson -> post it
21st Dec 2020, 2:14 PM
Steve Sajeev
Steve Sajeev - avatar
+ 5
Is this a tutorial for others instead of a question?
21st Dec 2020, 12:54 PM
Gordon
Gordon - avatar
+ 5
Actually U have answered the Question yourself ... The loop you have written in the description is the code to iterate through an array of numbers in Js .
21st Dec 2020, 12:55 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
I prefer to use predicate functions to perform any sort of recursive mathematical transformation on a data structure. My solution uses arrow functions. const newPrices = prices.map(n => n+amount);
21st Dec 2020, 1:18 PM
Ore
Ore - avatar
+ 2
No Steve Sajeev I don't have this intention to teach other but I want just to finish my courses in jscript so it is a way to understand more those topics
21st Dec 2020, 2:38 PM
HBhZ_C
HBhZ_C - avatar
+ 2
But still, please do not post this here. This is not the right place. Please understand..😊
22nd Dec 2020, 1:20 AM
Steve Sajeev
Steve Sajeev - avatar
+ 1
1. Your variant 2. for(let k of prices){k+=amount} Here k is element of array itself 3. prices= prices.map(val=>val+amount);
21st Dec 2020, 1:03 PM
george
george - avatar
+ 1
HBhZ_C Did you say "JScript" as in JScript, Microsoft's old JavaScript engine. I don't consider it a good choice if you are new to JavaScript. I advise that you first learn standard EcmaScript(ES 2020) before trying out any other implementation.
21st Dec 2020, 3:18 PM
Ore
Ore - avatar