Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/b923cc69b8ff4d61d06ef08b7e000a57 to your computer and use it in GitHub Desktop.
Save davidystephenson/b923cc69b8ff4d61d06ef08b7e000a57 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 (a, b) {
return a + b
}
var sum = add(3, 4)
console.log(sum)
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 (x) {
return x % 2 === 0
}
console.log(isEven(4))
console.log(isEven(5))
console.log("\n-------------------------------------------------\n");
// 3. Implement a function that checks if a given number is a palindrome (e.g., 121, 1331).
// 123321
function isPalindrome (x) {
if (x < 0) {
return false
}
var original = x
var reversed = 0
while (x > 0) {
var scaled = reversed * 10
var remainder = x % 10
reversed = scaled + remainder
x = (x - remainder) / 10
}
return original === reversed
}
console.log(isPalindrome(151))
console.log(isPalindrome(123))
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 = 'hi'
function test () {
var local = 'bye'
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 higher (callback) {
callback()
}
higher(() => console.log('lower'))
console.log("\n-------------------------------------------------\n");
// 6. IIFE Returning a Value
// Write an IIFE that returns the square of a number and assign the result to a variable.
(function () {
console.log('anonymous')
})()
console.log("\n-------------------------------------------------\n");
// 7. Arrow functions
// Define an arrow function that returns the cube of a number and assign the returned value to a variable.
var cube = x => x * x * x
var cubed = cube(5)
console.log(cubed)
console.log("\n-------------------------------------------------\n");
// 8. Arrow Function
// Write a function to print a right angle triangle of stars. Use arrow function which takes a number as a parameter that represents the number of lines in the triangle.
// *
// **
// ***
// ****
var rightAngleTriangle = (x) => {
for (var i = 1; i <= x; i++) {
var row = ''
for (var j = 1; j <= i; j++) {
row += '*'
}
console.log(row)
}
}
rightAngleTriangle(5)
console.log("\n-------------------------------------------------\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment