vendredi 26 février 2021

Why my transformation of a for-loop code into a while-loop one failed?

In the process of playing with getting all the odd numbers from 30 to 40 in a javascript's while-loop, I have encountered the problem of not being able to equally transform my for-loop code, which solves this task, into its while-loop counterpart. My while-loop gives out an extra '41'. Was my transformation wrong or while-loop is essentially different from the for-loop in JavaScript?

const max = 40;
const min = 30;
for (let i = min + 1; i < max; i += 2) {
  console.log(i);
}
// 31 33 35 37 39


const max = 40;
const min = 30;
let i = min + 1;
while (i < max) {
  console.log(i);
  i += 2;
}
// 31 33 35 37 39 41

Aucun commentaire:

Enregistrer un commentaire