Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/96da52fc56dbc1a7498463a6beb350c6 to your computer and use it in GitHub Desktop.
Save davidystephenson/96da52fc56dbc1a7498463a6beb350c6 to your computer and use it in GitHub Desktop.
// 1. Write a JavaScript program that prints the numbers from 1 to 10 using a for loop.
for (var i = 1; i <= 10; i++) {
console.log(i)
}
// 2. Write a JavaScript program that calculates the sum of all numbers from 1 to a given number using a while loop.
var sum = 0
var limit = 5
var x = 1
while (x <= limit) {
sum += x
x++
}
console.log(sum)
// 3. Write a JavaScript program that prints the even numbers from 1 to 20 using a do-while loop.
var x = 1
do {
const remainder = x % 2
const even = remainder == 0
if (even) {
console.log(x)
}
x++
} while (x <= 20)
// 4. Write a JavaScript program that prints the numbers from 10 to 0 using a for loop.
for (var i = 10; i >= 0; i--) {
console.log(i)
}
// 5. Write a JavaScript program that prints the alternating numbers from 1 to 10 using a for loop.
for (var i = 1; i <= 10; i += 2) {
console.log(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment