Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JS function task

The document provides examples of various programming loops and functions in JavaScript. It includes for loops, while loops, and functions with parameters, including default parameters and functions returning other functions. Each example demonstrates basic syntax and functionality for counting, iterating through arrays, and performing calculations.

Uploaded by

Rohan Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JS function task

The document provides examples of various programming loops and functions in JavaScript. It includes for loops, while loops, and functions with parameters, including default parameters and functions returning other functions. Each example demonstrates basic syntax and functionality for counting, iterating through arrays, and performing calculations.

Uploaded by

Rohan Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Basics for Loop


Write a for loop that counts from 1 to 10 and displays each number in the
console.
for (let i = 1; i <= 10; i++) {
console.log(i);
}

2. Loop Through an Array


Create an array of your favorite fruits and display each fruit.
let fruits = ["Apple", "Banana", "Cherry", "Mango", "Orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}

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");

7. Function with Multiple Parameters


Write a function calculateArea that calculates the area of a rectangle.
function calculateArea(length, width) {
return length * width;
}
console.log(calculateArea(5, 3));
console.log(calculateArea(7, 2));

8. Function with Default Parameter


Create a function sayHello with a default parameter.
function sayHello(name = "Guest") {
console.log(`Hello, ${name}!`);
}
sayHello("David");
sayHello();

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));

10. Function Returning a Function


Write a square function and use it inside a cube function.
function square(num) {
return num * num;
}
function cube(num) {
return square(num) * num;
}
console.log(cube(2));
console.log(cube(3));

You might also like