Created
September 20, 2025 16:53
-
-
Save davidystephenson/1604f3187b872220a02d048722111611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Question 1: Anonymous Function | |
| // Write an anonymous function that takes two numbers as parameters and returns their sum. | |
| var add = function (a, b) { | |
| return a + b | |
| } | |
| // Question 2: IIFE Function | |
| // Write an IIFE (Immediately Invoked Function Expression) that prints "Hello, World!" to the console. | |
| ;(function () { | |
| console.log('hello world') | |
| })() | |
| // Question 3: Scope - Global and Local | |
| // Write a function that demonstrates the concept of global and local scope. The function should have a local variable and a global variable, and it should print their values. | |
| var outer = 1 | |
| function scope () { | |
| var inner = 2 | |
| console.log(outer) | |
| console.log(inner) | |
| } | |
| scope() | |
| // Question 4: Anonymous Function as a Parameter | |
| // Write a function that takes an anonymous function as a parameter and invokes it. | |
| function announce (describe) { | |
| var result = describe() | |
| console.log('result:', result) | |
| } | |
| announce(() => { | |
| var random = Math.random() | |
| if (random > 0.5) { | |
| return 'hello' | |
| } else { | |
| return 'goodbye' | |
| } | |
| }) | |
| announce(() => 10) | |
| // Question 5: IIFE Returning a Value | |
| // Write an IIFE that returns the square of a number and assign the result to a variable. | |
| var result = (function (number) { | |
| return number * number | |
| })(5) | |
| console.log('result', result) | |
| // Question 6: Arrow functions | |
| // Define an arrow function that returns the cube of a number and assign the returned value to a variable. | |
| var cube = (number) => number * number * number | |
| var cubed = cube(2) | |
| console.log('cubed', cubed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment