Test your skills: Loops

the truth was a bit complicated for me when I had to read the DOM topic
I spent all afternoon trying to do exercise 1 but I don’t realize that I am doing it wrong.
Help xD

See the Pen JavaScript:Loops 1 by lTakisl (@ltakisl) on CodePen.

Hi @Takis :wave:

When you remove the if block around listA.textContent = myArray[i]; your code works. I’m not sure what you wanted to achieve whit this if block, but the result is always false as myArray[i] is a string that gets converted to a number which results in NaN (not a number).

Maybe you could explain what you wanted to achieve and I can clear up the confusion. :slightly_smiling_face:

Michael

I modified it there, thank you! not what happened was the following, as the live exercises in all had to put if () {something;} else {something;}
I assumed it should do the same, and didn’t realize that I had already expressed it with the for () loop. That is why I had tried to put if (myArray [i]> = 0) {that would create the elements li in the ul} ‘’ I think I had put that ‘’, so when going through the elements of the array if the element was greater than or equal a 0 inside the array would print the array element in a li inside the ul.
Something like that hahaha. Now that I put it into words I realize that I made a nice concoction of concepts and things hahaha.

1 Like

Hehe :grin:
Sometimes we think too complicated. Happens to all of us.
You will need if inside for in the next two examples. At least you now know how it works :slightly_smiling_face:

Michael

See the Pen JavaScript: Bucles 2 by lTakisl (@ltakisl) on CodePen.

In the third exercise I did not know how to do so that the loop that I had to do, it will use the isPrime () function so that it will give me as a result the number plus the text is prime in the case that it was. (I think I understand that was what I asked for.)
More or less I managed to get him to tell me what he had to give me but not in the way requested. Enlighten me XD

See the Pen JavaScript: Bucles 3 by lTakisl (@ltakisl) on CodePen.

Task 2 works as expected. Good job!
Small improvement: For objects instead of using “bracket notation” you can mostly use “dot notation” to make it simpler: phonebook[i]['name'] vs. phonebook[i].name

In task 3 you can replace num%2!== 0 with isPrime(num) to make it work. As isPrime() returns true for primes, the if block only gets executed for prime numbers. Below I wrote another solution with a different loop type. As a little bonus exercise you can try to understand it. :slightly_smiling_face:

do {
  if (isPrime(i)) {
    para.textContent += `${i} `;
  }
  i--;
} while (i > 1);

Feel free to ask if you need further help or explanations. Understanding loops is an important part of learning JavaScript. I’m happy to support you.

Happy coding!
Michael

See the Pen JavaScript: Bucles 3 by lTakisl (@ltakisl) on CodePen.

There I modified the exercise and I did it with the do while model. What if I had some doubts.
1.- why using a template literal when putting only the name of the variable does the same gives you the result? for something in particular?
2.- In this case I named the variables’ num ‘but, what if I had wanted to call them’ p ‘? Would I have changed all the’ num ‘to the isPrime function and replaced them with’ p ‘for the code to work with’ p '? including the ‘num’ of isPrime (num) for is Prime § right?

Yep, now it looks good :+1:

  1. I used a template string so I could include the space at the end in the same string. `${i} ` is the same as i + ' '. When you have longer strings with multiple variables it has a better readability, but in this case it doesn’t really matter. In my opinion it just looks more elegant. In your code you mixed template and traditional strings, which I wouldn’t recommend.
  2. You have to change all occurrences of num in and around your loop to p. The num of function isPrime(num) { ... } (parameter and inside of it) is another independent variable and can be left num or changed to whatever other name you like.

I hope my explanations are understandable.

Michael