For Loop exercise
Beginner Friendly Exercises for practicing JavaScript For and While Loops?
html - Exercise with a loop in javascript - Stack Overflow
Beginner working through Eloquent Javascript having a challenging time. Any suggestions for me?
Videos
I’ve been on a self-teaching journey for months learning html, css and now JavaScript. By some miracle, I’ve been able to learn the basics of some JavaScript concepts and unfortunately fell into a bit of a ditch with learning for and while loops in JavaScript. It was suggested to me (through a coding community) that I avoid practicing the blend of arrays and loops until I fully understand the basics of loops. So far I’ve been pulling through learning JavaScript through building super small and simplified things but I’ve been struggling to find good exercises online and via chat GPT that practice loops without the integration of arrays.
The only two possible exercises I saw was to
code a loop that counts to a certain number and
code a loop that displays a message beside all even numbers within a loop that has a range of numbers (i.e 1-10)
Does anybody else here learning web development have any valuable resources they can share surrounding loops?
I’m usually more of a visual learner and have a reputation for being horrible at math but I’m very determined to find a way to understand loops that makes sense in my ADHD brain and would appreciate any assistance. :)
I'm going to give you a "golfed-ish" (goldfish? should this be a thing?) version of the code. In other words, the smallest, most obscure code I can think of to accomplish the task. You should not use this, because your teacher will undoubtedly ask you what it means and you won't know, but I'm bored XD
var size = 5;
document.write("<center>"+Array.apply(0,new Array(size)).map(function(_,i) {return new Array((i+1)*2).join(" * ");}).join("<br>")+"</center>");
Demo
As I said, don't use this :p
Here is my code for you ...
<html>
<head>
<script type="text/javascript" language="javascript">
document.write("<center>"); //write a center tag to make sure the pyramid displays correctly(try it without this step to see what happens)
for(var i = 0; i <= 10; i++) //a loop, this counts from 0 to 10 (how many rows of stars)
{
for(var x = 0; x <= i; x++)// a loop, counting from 0 to whatever value i is currently on
{
document.write("*");//write a * character
}
document.write("<br/>"); //write a br tag, meaning new line, after every star in the row has been created
}
document.write("</center>"); //close the center tag, opened at the beginning
</script>
</head>
<body>
</body>
</html>