Collection of JavaScript exercises centered around Functions

Collection of JavaScript exercises centered around Functions

JavaScript Functions

https://basescripts.com/excited-to-share-a-collection-of-javascript-exercises-centered-around-functions

Exercise 1: Declaring a Function

Problem: Write a function named greet that prints "Hello, World!" to the console.

Explanation: This exercise introduces the basic syntax for declaring a function in JavaScript. Functions are reusable blocks of code that perform a specific task.

Code:

function greet() {

?console.log("Hello, World!");

}

greet(); // Call the function

Exercise 2: Function with Parameters

Problem: Create a function sum that takes two parameters, a and b, and logs their sum.

Explanation: Functions can take parameters, allowing you to pass values into them. This exercise demonstrates defining and calling a function with parameters.

Code:

function sum(a, b) {

?console.log(a + b);

}

sum(5, 7); // Outputs: 12

Exercise 3: Return Value

Problem: Write a function multiply that returns the product of two numbers.

Explanation: Functions can return values using the return statement. This value can then be used where the function is called.

Code:

function multiply(a, b) {

?return a * b;

}

console.log(multiply(6, 7)); // Outputs: 42

Exercise 4: Default Parameters

Problem: Create a function greet that takes a name as a parameter and greets the person. If no name is provided, it should greet "Guest".

Explanation: Default parameters allow you to initialize function parameters with default values if no value or undefined is passed.

Code:

function greet(name = "Guest") {

?console.log("Hello, " + name + "!");

}

greet("Alice"); // Outputs: Hello, Alice!

greet(); // Outputs: Hello, Guest!

Exercise 5: Arrow Functions

Problem: Convert a function expression add into an arrow function.

Explanation: Arrow functions provide a shorter syntax for writing function expressions. They are anonymous and change the way this binds in functions.

Code:

const add = (a, b) => a + b;

console.log(add(10, 5)); // Outputs: 15

Exercise 6: Anonymous Functions

Problem: Create an anonymous function that logs "I am anonymous!" and immediately invoke it.

Explanation: Anonymous functions are functions without a name. They can be used as IIFE (Immediately Invoked Function Expression) to execute code immediately.

Code:

(function() {

?console.log("I am anonymous!");

})();

Exercise 7: Callback Functions

Problem: Write a function processUserInput that takes a callback function as a parameter. The processUserInput function should call the callback function with a sample user input.

Explanation: Callback functions are passed to other functions as arguments and can be called within the outer function to complete some kind of routine or action.

Code:

function processUserInput(callback) {

?const name = "Alice";

?callback(name);

}

processUserInput(name => {

?console.log("Hello, " + name + "!");

});

Exercise 8: Function Scope

Problem: Create two variables in different scopes but with the same name, and log their values inside and outside a function.

Explanation: This exercise demonstrates how local and global scope work in JavaScript. Variables defined inside a function are not accessible from outside the function.

Code:

let animal = 'Lion'; // Global scope

function showAnimal() {

?let animal = 'Tiger'; // Local scope

?console.log(animal); // Tiger

}

showAnimal();

console.log(animal); // Lion

Exercise 9: Rest Parameters

Problem: Write a function sumAll that uses rest parameters to take any number of arguments and returns their sum.

Explanation: Rest parameters allow a function to accept an indefinite number of arguments as an array.

Code:

function sumAll(...numbers) {

?return numbers.reduce((acc, current) => acc + current, 0);

}

console.log(sumAll(1, 2, 3, 4)); // Outputs: 10

Exercise 10: Recursive Function

Problem: Write a recursive function factorial that calculates the factorial of a number.

Explanation: A recursive function is a function that calls itself until it doesn’t. The factorial of a number is the product of all positive integers less than or equal to the number.

Code:

function factorial(n) {

?if (n === 0) {

?return 1;

?} else {

?return n * factorial(n - 1);

?}

}

console.log(factorial(5)); // Outputs: 120

要查看或添加评论,请登录

Laurence Svekis ?的更多文章

社区洞察

其他会员也浏览了