Loops for and while javascript

Asked

Viewed 44 times

0

I was doing exercises to improve my logic and noticed that I can’t do decrement using the while loop?

cont = 20

while(cont > 20){
cont -= 1
console.log(cont)

```
sempre me retorna undefined, gostaria de saber o porque, já que se eu tentar fazer um incremento eu consigo com sucesso!!

outra questão usando o operador for

```
var array = ['a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','x','z']

for(var i= 0; i < array.length; i++){
    console.log(`${i} letra ${array.length[i]}`)
}

saida:
1 letra undefined
2 letra undefined
3 letra undefined
4 letra undefined
5 letra undefined
6 letra undefined
7 letra undefined
8 letra undefined
9 letra undefined
// .....

o operador for percorre o array e me retorna a quantidade de índices do array, mas não o valor... como faria para ter o valor retornado juntamente com o indice?

  • Do not put compliments or thanks in questions. If you have to put something outside the technical scope use the comments. Ref-> https://answall.com/help/behavior

1 answer

4


the problem with the while is that the condition is never true, so the loop never turns around.

as Count starts with 20, and compare if it is less than 20, this is false.


about for, you need to print the position:

console.log(`${i} letra ${array[i]}`)

and not:

console.log(`${i} letra ${array.length[i]}`)
  • thank you very much as to while I achieve the result I would like using DO/WHILE

Browser other questions tagged

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