Recap
Previously I did two posts on common questions you may encounter during JS interviews. You can find the posts her: part 1 and part 2
Disclaimer
As usual, the solutions discussed below, assume that you already have an understanding of Javascript and specifically data types and OOP.
The solutions may also not be the best and if you have a better more effective solution, please make a Pull Request to this repo, and I will add your solution.
Sample Challenges
Challenge 12 - Add all arguments
Given n number of arguments, Return a sum of all parameters entered regardless of the amount of numbers - NO ARRAYS
function addAll() {
//? solution 1 - es5 arguements object and for loop
var args = Array.prototype.slice.call(arguments);
var total = 0;
for (let i = 0; i < args.length; i++) {
total += args[i];
}
return total;
}
using
...rest
operator andforEach
function addAll(...numbers) {
//? solution 2a - es6 - ...rest operator and forEach
let total = 0;
numbers.forEach(num => (total += num));
return total;
}
function addAll(...numbers) {
//? solution 2b - es6 - ...rest operator and reduce
return numbers.reduce((a, b) => a + b);
}
Challenge 13 - Sum all primes
Given a number n, add all of the prime numbers. A prime number is a whole number greater than 1 whose only factors are 1 and itself
for this solution we will need a helper that helps check if a number is prime.
// helper function
function checkForPrime(n) {
for (let j = 2; j < n; j++) {
if (n % j === 0) {
return false;
}
}
return true;
}
function sumAllPrimes(num) {
let sum = 0;
for (let i = 2; i <= num; i++) {
if (checkForPrime(i)) {
sum += i;
}
}
return sum;
}
Challenge 14 - Seek and destroy
Remove from the array whatever is in following arguments. Return the leftover numbers in an array
function seekAndDestroy(arr) {
//? solution 1 - using arguments, indexOf, filter
const args = Array.from(arguments);
function filterArr(arr) {
// Return true if NOT in array
return args.indexOf(arr) === -1;
}
return arr.filter(filterArr);
}
using
...rest
operator, includes
function seekAndDestroy(arr, ...rest) {
//? solution 2 - ...rest, filter, includes
return arr.filter(val => !rest.includes(val));
}
Challenge 15 - Sort by Height
Some people are standing in a row in a park. There are trees between them which cannot be moved. Given an array of the heights, your task is to rearrange the people by their heights in a non-descending order without moving the trees.
Assume the tree heights are represented by negative numbers
(this is actually a common whiteboard question)
function sortByHeight(a) {
const arr1 = [];
const arr2 = [];
a.forEach((val, i) => val === -1 ? arr1.push(i) : arr2.push(val));
// sort arr2 ascending
const sortArr = arr2.sort((a, b) => a - b);
// use array splice to insert tree heights
arr1.forEach((x, i) => sortArr.splice(arr1[i], 0, -1));
return sortArr;
}
Challenge 16 - Missing letters
Given a string of letters, find the missing letter and return it. If all letters are present, return undefined
function missingLetters(str) {
let compare = str.charCodeAt(0);
let missing;
str.split('').map((char, i) => {
if (str.charCodeAt(i) == compare) {
++compare;
} else {
missing = String.fromCharCode(compare);
}
});
return missing;
}
Challenge 17 - Even & Odd Sums
Given an array of numbers, return an array of the sums of even and odd numbers.
function evenOddSums(arr) {
let evenSum = 0;
let oddSum = 0;
arr.forEach(num => (num % 2 === 0 ? (evenSum += num) : (oddSum += num)));
return [evenSum, oddSum];
}
That's all for today. I will keep updating this series. Perhaps in the future, I will also do a series on python and dart too.
Till next time, have a great time and take care.
Remember if you come up with better solutions, please make a pull request on this repo.
Top comments (0)