Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/10a800bfcb0ba57347843ab316639fd2 to your computer and use it in GitHub Desktop.
Save davidystephenson/10a800bfcb0ba57347843ab316639fd2 to your computer and use it in GitHub Desktop.
<script>
// 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) {
const remainder = x % 2
return remainder === 0
}
console.log(isEven(4))
console.log(isEven(7))
console.log("\n-------------------------------------------------\n");
// 3. Implement a function that checks if a given number is a palindrome (e.g., 121, 1331).
function isPalindrome (x) {
var original = x
var reversed = 0
while (x > 0) {
reversed = reversed * 10
var next = x % 10
reversed = reversed + next
x = (x - next) / 10
}
return original === reversed
}
console.log(isPalindrome(123))
console.log(isPalindrome(121))
console.log("\n-------------------------------------------------\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment