Created
May 17, 2025 16:41
-
-
Save davidystephenson/050309d39fc07d9f7ee2dc4d0c059e5f 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 (x, y) { | |
return x + y | |
} | |
console.log(add(10, 5)) | |
console.log("\n-------------------------------------------------\n"); | |
// 2. Write a function `isEven` that takes a number as a parameter and returns `true` if it is even, and `false` otherwise. | |
function isEven (number) { | |
var remainder = number % 2 | |
var even = remainder === 0 | |
return even | |
} | |
console.log(isEven(4)) | |
console.log(isEven(11)) | |
console.log("\n-------------------------------------------------\n"); | |
// 3. Implement a function that checks if a given number is a palindrome (e.g., 121, 1331). | |
function isPalindrome (number) { | |
console.log(number) | |
const string = String(number) | |
console.log(string) | |
const array = string.split('') | |
console.log(array) | |
array.reverse() | |
console.log(array) | |
const joined = array.join('') | |
console.log(joined) | |
return string === joined | |
} | |
console.log(isPalindrome(123)) | |
console.log(isPalindrome(54845)) | |
console.log("\n-------------------------------------------------\n"); | |
// 4. 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 global = 'global' | |
function test () { | |
var local = 'local' | |
console.log(global) | |
console.log(local) | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment