1) Five students enter the classroom one after another. The first student gets roll number 1, the second gets 2, and so on. Display the assigned roll numbers.
let students = 5;
for(let i = 1; i <= students; i++){
console.log(i);
}
Output:
1
2
3
4
5
2) A coach asks students to stand only at even-numbered positions in a queue. Display the first five positions.
let reqPositions = 5;
(without if condition)
for(let i = 2; i <= reqPositions * 2; i += 2){
console.log(i);
}
(with if condition)
for(let i = 1; i <= reqPositions * 2; i++){
if(i % 2 === 0){
console.log(i);
}
}
Output:
2
4
6
8
10
3) A newly constructed building has a special lift that does not stop on every floor. For safety reasons, it stops only at every third floor. A person enters the lift at the ground floor and presses the "Up" button. Display the first five floors where the lift will stop. (TBD)
let reqFloors = 5;
(without if condition)
for(let i = 0; i <= reqFloors * 3; i += 3){
console.log(i);
}
(with if condition)
for(let i = 0; i <= reqFloors * 3; i++){
if(i % 3 === 0){
console.log(i);
}
}
Output:
0
3
6
9
12
15
4) A rocket launches with a countdown using only even numbers from 10. Display the countdown.
(without if condition)
for(let i = 10; i >= 0; i -= 2){
console.log(i);
}
(with if condition)
for(let i = 10; i >= 0; i--){
if(i % 2 === 0){
console.log(i);
}
}
Output:
10
8
6
4
2
0
Top comments (0)