Skip to content

Level 2

Palindrome Check Function

function isPalindrome(str) {
return str === str.split("").reverse().join("");
}
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

function filterEvenNumbers(numbers) {
return numbers.filter((num) => num % 2 === 0);
}
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

function addElem(array, el) {
return [...array, el];
}
Answer

Explanation:

The addElem function adds an element to an array without mutating the original array by using the spread operator.


Concatenate Two Arrays

function concat(arr1, arr2) {
return [...arr1, ...arr2];
}
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

function checkName(name, arr) {
return arr.some((obj) => obj.name === name);
}
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

let unique = (arr) => {
return [...new Set(arr)];
};
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

function uniqueFinder(target) {
return target.reduce((cumul, el) => {
return cumul.includes(el) ? cumul : [...cumul, el];
}, []);
}
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

let counter = 0;
for (var i = 1; i <= 10; i++) {
counter += i;
}
console.log(counter);
console.log(i);
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

const object1 = {
a: 10,
b: 20,
c: function () {
console.log(this.a + this.b);
},
};
const func = object1.c;
func();
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

function factorial(number) {
if (number === 0 || number === 1) {
return 1;
} else {
return number * factorial(number - 1);
}
}
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

let a = 0;
let b = 1;
function createFibonacciGenerator() {
return function () {
const result = a;
const temp = a + b;
a = b;
b = temp;
return result;
};
}
const caller = createFibonacciGenerator()
caller();
caller();
caller();

// Modify this so that, when passed an index get that fibnacci value

let a = 0;
let b = 1;
let fibnacciSeries = []
function getFibnacciAt(index) {
for(let i = 0 ; i <= index; i++){
let result = a ;
let temp = a + b;
a = b ;
b = temp;
fibnacciSeries = [...fibnacciSeries, result]
}
console.log(fibnacciSeries[index])
}
getFibnacciAt(25);
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

const books = [
{ name: "Harry Potter", author: "Joanne Rowling" },
{ name: "Warcross", author: "Marie Lu" },
{ name: "The Hunger Games", author: "Suzanne Collins" },
];
let sorted = books.sort((book1, book2) => {
let author1 = book1.author.split(" ")[1];
let author2 = book2.author.split(" ")[1];
return author1 > author2 ? -1 : 1;
});
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:

let sorted = books.sort((book1, book2) => {
let author1 = book1.author.split(" ")[1];
let author2 = book2.author.split(" ")[1];
return author1 < author2 ? -1 : 1;
});

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

console.log(some([2,4,6], 3, isEven)) // should print true
console.log(some([2,3,4], 3, isEven)) // should print false
console.log(some([2,3,11], 4, isPrime)) // should print false
console.log(some([2,3,5,9], 3, isPrime)) // should print true
some(array, n, conditionFunction) -> returns true Or False
array - Input array
n - The function should check if n elements of the conditionFunction satisfy
Answer

Explanation:

function some(array, n, conditionFunction) {
let mapped = array.map((i) => conditionFunction(i));
return mapped.filter((i) => i === true).length >= n;
}
function isEven(int) {
return int % 2 === 0;
}
function isPrime(int) {
for (let i = 2, s = Math.sqrt(int); i <= s; i++) {
if (int % i === 0) return false;
}
return int > 1;
}