JS function task
JS function task
3. While Loop
Write a while loop that counts from 1 to 5 and displays each number.
let count = 1;
while (count <= 5) {
console.log(count);
count++;
}
4. Looping Backwards
Create an array of numbers from 10 to 1 and display them.
let numbers = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
5. Loop with Conditional Statement
For each number from 1 to 20, check if it's even or odd.
for (let i = 1; i <= 20; i++) {
if (i % 2 === 0) {
console.log(`${i} is even`);
} else {
console.log(`${i} is odd`);
}
}
6. Function Basics
Create a function greet that takes a name as a parameter and displays a greeting
message.
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Alice");
greet("Bob");
greet("Charlie");
9. Function as a Variable
Write a function multiply that multiplies two numbers and assign it to a variable.
let multiply = function(a, b) {
return a * b;
};
let operation = multiply;
console.log(operation(3, 4));