8000 GitHub - bordaf90/javascript-coding-challenges: JavaScript Coding Challenges for Beginners
[go: up one dir, main page]

Skip to content

bordaf90/javascript-coding-challenges

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

javascript

JavaScript Coding Challenges for Beginners

Multiples of 3 or 5

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;
};

⬆ Back to Top

Even or Odd

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 => {
  return number % 2 === 0 ? 'Even' : 'Odd';
};

⬆ Back to Top

Clock

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.

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;
}

⬆ Back to Top

Returning Strings

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 => {
  return `Hello, ${name} how are you doing today?`;
}

⬆ Back to Top

Century From Year

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); 
}

⬆ Back to Top

Keep Hydrated!

Nathan loves cycling. Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling. You get given the time in hours and you need to return the number of litres 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 / 2);
}

⬆ Back to Top

Is n Divisible by x and y?

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);
}

⬆ Back to Top

Vowel Count

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;
};

⬆ Back to Top

Disemvowel Trolls

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 => {
  return str.replace(/[aeiou]/gi, '');
}

⬆ Back to Top

Find the Odd Int

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, 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);
}

⬆ Back to Top

Get the Middle Character

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 midIndex = str.length / 2 ;
  return str.length % 2 ? str[Math.floor(midIndex)] : str[midIndex - 1] + str[midIndex];
}

⬆ Back to Top

Who likes it?

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 => {
  let output;
  if (names.length === 0) {
    output = 'no one likes this';
  } else if (names.length === 1) {
    output = `${names[0]} likes this`;
  } else if (names.length === 2) {
    output = `${names[0]} and ${names[1]} like this`;
  } else if (names.length === 3) {
    output = `${names[0]}, ${names[1]} and ${names[2]} like this`;
  } else {
    output = `${names[0]}, ${names[1]} and ${names.length - 2} others like this`;
  }
  return output;
}

⬆ Back to Top

Create Phone Number

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 RegEx
  return numbers.join('').replace(/(\d{3})(\d{3})(\d+)/, '($1) $2-$3');
  
  // Using reduce()
  // return numbers.reduce((acc, cur) => acc.replace('x', cur), '(xxx) xxx-xxxx');
}

⬆ Back to Top

Square Every Digit

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(''));
}

⬆ Back to Top

About

JavaScript Coding Challenges for Beginners

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0