What you want is not to count the items, because it is difficult to tell something without knowing how many you have, and knowing how many you have does not need to count.
To tell something you don’t know how many exist only if you have a clear terminator, which is not the case of a array javascript.
The statement asks to do one of the simplest things with the array which is to sweep it all with a loop. In fact it has no different to do. You can even use a function that abstracts that, but in practice it will have a loop inside it.
See on documentation how does the for - of
whose function is to just walk element by element and give you a variable to do whatever you want with it during the execution of that step. This way is fast and safe, as well as very simple.
Exercise does not ask to do something complex.
for (var item of ['aegir', 'aud', 'balder']) alert(item);
If you want to do it by hand it could be like this:
var deuses = ['aegir', 'aud', 'balder'];
var i = 0;
while (i < deuses.length) { //vai até o tamanho já conhecido
alert(deuses[i]); //pega o elemento indexado pela variável de controle do laço
i++; //incrementa a variável para o próximo passo
}
I put in the Github for future reference.
But you take risks. This is a case that seems obvious, but I see a lot of people making mistakes even in something simple like this, especially going beyond the size.
Explain better why you need to do this, because you don’t really need any tie.
– Maniero
If you only want the size of the array you don’t need a loop, just use
alert(deuses.length);
– Ricardo Pontual
In the exercise asks me to do using each repeat loop.
– BrunoSuarez
It’s strange because, to make the loop, you already need to know the size:
(while i< deuses.lengh)
– Ricardo Pontual
Place the statement.
– Maniero
(37.b) Create the following array: var gods = ['Aegir', 'Aud', 'Balder'] E display one by one with Alert() - Preferred for repeat loops (for, foreach, while, do{}while)
– BrunoSuarez
(37.b) Create the following array: var gods = ['Aegir', 'Aud', 'Balder'] E display one by one with Alert() - Preferred for repeat loops (for, foreach, while, do{}while)
– BrunoSuarez
Nothing in it indicates to count the elements. It seems to me to be a problem of text interpretation.
– Maniero
I think the exercise should ask to iterate in the array and go counting.
– Marconi
It seems to me that the exercise asks to display the elements with an Alert() using while
– codermarcos
@Brunosuarez The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero