Get Programming with JavaScript - Listing 19.05
Listing 19.05 - Using a while loop to count
JS Bin
var count = 1;
while (count < 11) {
console.log(count);
count++;
}
Further Adventures
Listing 19.05 - Using a while loop to count - Task 2
- Change the program so it counts down from 10 to 1.
var count = 10;
while (count > 0) {
console.log(count);
count--;
}
Listing 19.05 - Using a while loop to count - Task 3
- Change it so it shows all the odd numbers from 1 to 100.
var count = 1;
while (count < 100) {
console.log(count);
count += 2;
}
Listing 19.05 - Using a while loop to count - Tasks 4 & 5
- Create an array of numbers and assign it to a variable.
- Use a while loop to display the elements of the array.
var count = 0;
var nums = [2, 7, 1, 9];
var numsLength = nums.length;
while (count < numsLength) {
console.log(nums[count]);
count++;
}