If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0.
const solution = number => {
// Your solution
};
console.log(solution(0)); // 0
console.log(solution(-15)); // 0
console.log(solution(10)); // 23
console.log(solution(20)); // 78
console.log(solution(200)); // 9168
Solution
const solution = number => {
let sum = 0;
for (let i = 3; i < number; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
return sum;
};
Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
const even_or_odd = number => {
// Your solution
};
console.log(even_or_odd(0)); // 'Even'
console.log(even_or_odd(2)); // 'Even'
console.log(even_or_odd(3)); // 'Odd'
console.log(even_or_odd(-3)); // 'Odd'
Solution
const even_or_odd = number => {
// Let's use a ternary operator
return number % 2 === 0 ? 'Even' : 'Odd';
};
The clock shows h hours (0 <= h <= 23), m minutes (0 <= m <= 59) and s seconds (0 <= s <= 59) after midnight. Your task is to write a function which returns the time since midnight in milliseconds. There are 1,000 milliseconds in a second.
const past = (h, m, s) => {
// Your solution
};
console.log(past(0, 0, 0)); // 0
console.log(past(0, 1, 1)); // 61000
console.log(past(1, 0, 0)); // 3600000
console.log(past(1, 0, 1)); // 3601000
console.log(past(1, 1, 1)); // 3661000
Solution
const past = (h, m, s) => {
return (h * 60 * 60 + m * 60 + s) * 1000;
};
Write a function that given the input string name
, returns the greeting statement Hello, <name> how are you doing today?
const greet = name => {
//Your solution
};
console.log(greet('Ryan')); // "Hello, Ryan how are you doing today?"
console.log(greet('Sara')); // "Hello, Sara how are you doing today?"
Solution
const greet = name => {
// Let's use a template literal
return `Hello, ${name} how are you doing today?`;
};
The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc. Given a year, return the century it is in.
const century = year => {
// Your solution
};
console.log(century(1705)); // 18
console.log(century(1900)); // 19
console.log(century(1601)); // 17
console.log(century(2000)); // 20
console.log(century(89)); // 1
Solution
const century = year => {
return Math.ceil(year / 100);
};
Nathan loves cycling. Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling. Given the time in hours, you need to return the number of litres of water that Nathan will drink, rounded to the smallest value.
const litres = time => {
// Your solution
};
console.log(litres(0)); // 0
console.log(litres(2)); // 1
console.log(litres(1.4)); // 0
console.log(litres(12.3)); // 6
console.log(litres(0.82)); // 0
console.log(litres(11.8)); // 5
console.log(litres(1787)); // 893
Solution
const litres = time => {
return Math.floor(time * 0.5);
};
Create a function that checks if a number n
is divisible by two numbers x
AND y
. All inputs are positive, non-zero digits.
const isDivisible = (n, x, y) => {
// Your solution
};
console.log(isDivisible(3, 3, 4)); // false
console.log(isDivisible(12, 3, 4)); // true
console.log(isDivisible(8, 3, 4)); // false
console.log(isDivisible(48, 3, 4)); // true
Solution
const isDivisible = (n, x, y) => {
return n % x === 0 && n % y === 0;
};
Return the number (count) of vowels (a, e, i, o, u) in the given string. The input string will only consist of lower case letters and/or spaces.
const getCount = str => {
// Your solution
};
console.log(getCount('my pyx')); // 0
console.log(getCount('pear tree')); // 4
console.log(getCount('abracadabra')); // 5
console.log(getCount('o a kak ushakov lil vo kashu kakao')); // 13
Solution
const getCount = str => {
let vowelsCount = 0;
for (let char of str) {
if ('aeiou'.includes(char)) vowelsCount++;
}
return vowelsCount;
};
// An alternative solution could be to convert the string to an array (using the spread syntax)
// and filtering that array by vowels
const getCount = str => {
const arr = [...str];
return arr.filter(ele => ['a', 'e', 'i', 'o', 'u'].includes(ele)).length;
};
Trolls are attacking your comment section! A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat. Your task is to write a function that takes a string and returns a new string with all vowels (a, e, i, o, u
) removed.
const disemvowel = str => {
// Your solution
};
console.log(disemvowel('This website is for losers LOL!')); // 'Ths wbst s fr lsrs LL!'
Solution
const disemvowel = str => {
// Let's use regular expressions (regex)
return str.replace(/[aeiou]/gi, '');
};
Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times.
const findOdd = arr => {
// Your solution
};
console.log(findOdd([20, 1, -1, 2, -2, 3, 3,
10000
5, 5, 1, 2, 4, 20, 4, -1, -2, 5])); // 5
console.log(findOdd([20, 1, 1, 2, 2, 3, 3, 5, 5, 4, 20, 4, 5])); // 5
console.log(findOdd([1, 1, 2, -2, 5, 2, 4, 4, -1, -2, 5])); // -1
console.log(findOdd([5, 4, 3, 2, 1, 5, 4, 3, 2, 10, 10])); // 1
console.log(findOdd([1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1])); // 10
console.log(findOdd([10])); // 10
Solution
const findOdd = arr => {
return arr.reduce((a, b) => a ^ b);
};
Given a word, your job is to return the middle character(s) of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
const getMiddle = str => {
// Your solution
};
console.log(getMiddle('test')); // 'es'
console.log(getMiddle('testing')); // 't'
console.log(getMiddle('middle')); // 'dd'
console.log(getMiddle('A')); // 'A'
Solution
const getMiddle = str => {
const len = str.length;
const mid = len / 2;
// For an odd length, len % 2 equals 1 which is truthy
return len % 2 ? str[Math.floor(mid)] : str[mid - 1] + str[mid];
};
You probably know the "like" system from Facebook and other social media. People can "like" posts, photos or other items. We want to create the text that should be displayed next to such an item.
Implement a function that takes an input array, containing the names of people who like an item and returns an output string formatted nicely as shown below.
const likes = names => {
// Your solution
};
console.log(likes([])); // 'no one likes this'
console.log(likes(['Peter'])); // 'Peter likes this'
console.log(likes(['Jacob', 'Alex'])); // 'Jacob and Alex like this'
console.log(likes(['Max', 'John', 'Mark'])); // 'Max, John and Mark like this'
console.log(likes(['Alex', 'Jacob', 'Mark', 'Max'])); // 'Alex, Jacob and 2 others like this'
Solution
const likes = names => {
const len = names.length;
let output;
if (len === 0) {
output = 'no one likes this';
} else if (len === 1) {
output = `${names[0]} likes this`;
} else if (len === 2) {
output = `${names[0]} and ${names[1]} like this`;
} else if (len === 3) {
output = `${names[0]}, ${names[1]} and ${names[2]} like this`;
} else {
output = `${names[0]}, ${names[1]} and ${len - 2} others like this`;
}
return output;
};
Write a function that accepts an array of 10 integers (between 0 and 9), and returns a string of those numbers in the form of a phone number.
const createPhoneNumber = numbers => {
// Your solution
};
console.log(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])); // '(123) 456-7890'
console.log(createPhoneNumber([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])); // '(111) 111-1111'
console.log(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])); // '(123) 456-7890'
Solution
const createPhoneNumber = numbers => {
// Using substrings
const str = numbers.join('');
return `(${str.substring(0, 3)}) ${str.substring(3, 6)}-${str.substring(6)}`;
// Alternative solution using RegEx
// return numbers.join('').replace(/(\d{3})(\d{3})(\d+)/, '($1) $2-$3');
// Alternative solution using reduce()
// return numbers.reduce((acc, cur) => acc.replace('x', cur), '(xxx) xxx-xxxx');
};
Given an integer, your task is to square every digit of it and concatenate them to produce a new integer.
const squareDigits = num => {
// Your solution
};
console.log(squareDigits(2112)); // 4114
console.log(squareDigits(3212)); // 9414
console.log(squareDigits(9159)); // 8112581
Solution
const squareDigits = num => {
return Number(
num
.toString()
.split('')
.map(ele => ele * ele)
.join('')
);
};
Write a function that given an integer, checks to see if it is a square number. A square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself.
const isSquare = n => {
// Your solution
};
console.log(isSquare(0)); // true
console.log(isSquare(4)); // true
console.log(isSquare(25)); // true
console.log(isSquare(3)); // false
console.log(isSquare(93)); // false
console.log(isSquare(-1)); // false
Solution
const isSquare = n => {
return Math.sqrt(n) % 1 === 0;
};
Given a string of space-separated numbers, write a function that returns the highest and lowest numbers. There will always be at least one number in the input string.
const highAndLow = numbers => {
// Your solution
};
console.log(highAndLow('1 2 3 4 5')); // '5 1'
console.log(highAndLow('1 2 -3 4 5')); // '5 -3'
console.log(highAndLow('1 9 3 4 -5')); // '9 -5'
console.log(highAndLow('0 -214 542')); // '542 -214'
Solution
const highAndLow = numbers => {
const arr = numbers.split(' ');
return `${Math.max(...arr)} ${Math.min(...arr)}`;
};
Write a function that takes any non-negative integer as an argument and returns it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.
const descendingOrder = n => {
// Your solution
};
console.log(descendingOrder(0)); // 0
console.log(descendingOrder(1)); // 1
console.log(descendingOrder(1021)); // 2110
console.log(descendingOrder(42145)); // 54421
console.log(descendingOrder(145263)); // 654321
console.log(descendingOrder(123456789)); // 987654321
Solution
const descendingOrder = n => {
return parseInt(
n
.toString()
.split('')
.sort((a, b) => b - a)
.join('')
);
};
Given a string which includes only letters, write a function that produces the outputs below.
const accum = str => {
// Your solution
};
console.log(accum('abcd')); // 'A-Bb-Ccc-Dddd'
console.log(accum('cwAt')); // 'C-Ww-Aaa-Tttt'
console.log(accum('RqaEzty')); // 'R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy'
Solution
const accum = str => {
return str
.split('')
.map((ele, index) => ele.toUpperCase() + ele.toLowerCase().repeat(index))
.join('-');
};
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed. Strings passed in will consist of only letters and spaces.
const spinWords = str => {
// Your solution
};
console.log(spinWords('This is a test')); // 'This is a test'
console.log(spinWords('Hey fellow warriors')); // 'Hey wollef sroirraw'
console.log(spinWords('This is another test')); // 'This is rehtona test'
Solution
const spinWords = str => {
return str
.split(' ')
.map(word => (word.length < 5 ? word : word.split('').reverse().join('')))
.join(' ');
};
Given a non-empty string of words, return the length of the shortest word(s).
const findShort = str => {
// Your solution
};
console.log(findShort('Test where final word shortest see')); // 3
console.log(findShort('Lets all go on holiday somewhere very cold')); // 2
console.log(findShort('i want to travel the world writing code one day')); // 1
Solution
const findShort = str => {
return Math.min(...str.split(' ').map(word => word.length));
};
Write a function that takes an integer as input, and returns the number of bits that are equal to 1
in the binary representation of that number. You can guarantee that input is non-negative. For example the binary representation of 1234
is 10011010010
, so the function should return 5
in this case.
const countBits = n => {
// Your solution
};
console.log(countBits(0)); // 0
console.log(countBits(4)); // 1
console.log(countBits(7)); // 3
console.log(countBits(9)); // 2
Solution
const countBits = n => {
return n.toString(2).split('0').join('').length;
};