Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/2421819cd6a0b66af0b271c8f9896a32 to your computer and use it in GitHub Desktop.
Save davidystephenson/2421819cd6a0b66af0b271c8f9896a32 to your computer and use it in GitHub Desktop.
// 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()
console.log("\n-------------------------------------------------\n");
// 5. Anonymous Function as a Parameter
// Write a function that takes an anonymous function as a parameter and invokes it.
function parent (child) {
console.log('before')
child()
console.log('after')
}
parent(() => console.log('I am the child'))
console.log("\n-------------------------------------------------\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment