Javascript loop help. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Javascript loop help.

c.anything(20,20,50,50); c.anything(30,20,50,50); c.anything(40,20,50,50); c.anything(50,20,50,50); I want to type this code only once. and want to change the first value for a certain times like 20 times. I need to create a loop now .but couldn't figure it out. #javascript help

19th Jul 2018, 9:18 AM
Nayem
7 Answers
+ 4
I'm not sure if I understand what you want to do. Your code actually creates 300 elements. They're just overlapping. Check this code. I'm not sure if this is what you want to do https://code.sololearn.com/W0AeTdS4UZ85/?ref=app
19th Jul 2018, 10:08 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 2
You can do: for(var x=20; x<60; x+=10){ c.anything(x,20,50,50); }
19th Jul 2018, 9:29 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
19th Jul 2018, 10:30 AM
Nayem
+ 1
for(var i = 0;i<300;i+=50){ var canvas = document.querySelector("canvas"); canvas.width = window.innerWidth; canvas.height=window.innerHeight; var c = canvas.getContext("2d"); c.fillStyle="#333"; c.fillRect(i,20,30,30); } it should creat a line but its just creating 1 element
19th Jul 2018, 9:53 AM
Nayem
+ 1
https://code.sololearn.com/WmOj78vUoLxR/?ref=app check this I want it to start from width 50 and end at 250 and it should be continued
19th Jul 2018, 9:56 AM
Nayem
+ 1
I finally did what I wanted to do. It was not that tough but I didn't knew that could be done by this. https://code.sololearn.com/WWkIr57N92G2/#html
19th Jul 2018, 1:07 PM
Nayem
0
Thanks again for helping me and if you have any recommendation to look it good suggest me please.
19th Jul 2018, 1:09 PM
Nayem