Level 2
Palindrome Check Function
Answer
Explanation:
The function isPalindrome
checks if a string is the same when read forwards and backwards. It does this by splitting the string into an array of characters, reversing that array, and joining it back into a string to compare with the original.
Return Even Numbers in an Array
Answer
Explanation:
The function filterEvenNumbers
returns a new array containing only the even numbers from the input array. It uses the filter
method to check if each number is even.
Add Element to Array
Answer
Explanation:
The addElem
function adds an element to an array without mutating the original array by using the spread operator.
Concatenate Two Arrays
Answer
Explanation:
The concat
function concatenates two arrays using the spread operator, creating a new array with elements from both input arrays.
Check if Name is in Array of Objects
Answer
Explanation:
The checkName
function checks if a given name exists in an array of objects. It uses the some
method to see if any object in the array has a name
property that matches the given name.
Return Unique Elements - Method 1
Answer
Explanation:
The unique
function returns an array with only unique elements by creating a Set from the input array and then spreading it back into a new array.
Return Unique Elements - Method 2
Answer
Explanation:
The uniqueFinder
function returns an array with only unique elements by using the reduce
method to accumulate unique elements.
Predict Output Example - 1
Answer
Explanation:
The counter
variable will be 55, as it sums the numbers from 1 to 10. The variable i
will be 11, as it increments after the last iteration of the loop.
Predict Output Example - 2
Answer
Explanation:
The func
variable will not work as expected because it loses the context of this
. The this
value inside func
will not refer to object1
, causing it to print NaN
. Using bind
to retain the context will fix the issue.
object1.c()
will work, if assiged to another varable func, this value of this will change OR
const func = object1.c.bind(object1); func();
will work perfectly.
Factorial Function
Answer
Explanation:
The factorial
function calculates the factorial of a number recursively. If the number is 0 or 1, it returns 1. Otherwise, it multiplies the number by the factorial of the number minus one.
Fibonacci Generator
// Modify this so that, when passed an index get that fibnacci value
Answer
Explanation:
The createFibonacciGenerator
function returns a function that generates Fibonacci numbers. Each call to the returned function produces the next number in the Fibonacci sequence.
Sort Array of Objects by Author’s Last Name
Answer
Explanation:
The sorted
variable holds an array of books sorted by the author’s last name. It splits each author’s name and compares the last names to determine the order.
The expression author1 > author2 ? -1 : 1 checks if author1 is greater than author2. If true, it returns -1, indicating that author1 should come before author2. Otherwise, it returns 1, indicating that author2 should come before author1.
Sorting Order:
The comparison function is called for pairs of elements in the array to determine their order. This particular comparison sorts the array in descending order because it uses -1 for author1 > author2. To sort in ascending order, you can modify the comparison function:
This will sort the array based on the authors’ last names in ascending order.
Custom function
Write the some function and isEven and isPrime functions according to the following conditions
Answer
Explanation: