Array counter with while in JS

Asked

Viewed 851 times

1

I need to count the elements of a array using the loop while and display in a alert(), but is not exhibiting in the browser the count number. Using for worked out.

I leave my code below:

var deuses = new Array();
  deuses = ['aegir', 'aud', 'balder'];

  var i = 0;

   while (i < deuses){
        alert(deuses.length);
   i++;
}
  • Explain better why you need to do this, because you don’t really need any tie.

  • If you only want the size of the array you don’t need a loop, just use alert(deuses.length);

  • In the exercise asks me to do using each repeat loop.

  • It’s strange because, to make the loop, you already need to know the size: (while i< deuses.lengh)

  • Place the statement.

  • (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)

  • (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)

  • Nothing in it indicates to count the elements. It seems to me to be a problem of text interpretation.

  • I think the exercise should ask to iterate in the array and go counting.

  • It seems to me that the exercise asks to display the elements with an Alert() using while

  • @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).

Show 6 more comments

1 answer

2

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.

  • Thank you so much for helping Maniero.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.