Skip to content

Instantly share code, notes, and snippets.

@jlozovei
Created February 14, 2023 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlozovei/24f018adfdcf24ed31c7f3f55776cab6 to your computer and use it in GitHub Desktop.
Save jlozovei/24f018adfdcf24ed31c7f3f55776cab6 to your computer and use it in GitHub Desktop.
Coding interviews exercises and solutions
  1. Create a function that divides an array of numbers into odd and even.

Input: [0, 1, 2, 3, 4, 5]

Output: { odd: [0, 2, 4], even: [1, 3, 5] }

  1. Create a function that verifies how many times a number is repeated within an array.

Input: [0, 1, 1, 2, 3, 4, 5, 2, 3, 5, 7, 8, 6]

Output: Number X is repeated Y times

  1. Create a function that displays the missing value of an array.

Input: [0, 1, 2, 3, 4, 5, 7]

Output: The number 6 is missing

  1. Create a function that divides an array of numbers into odd and even.
function divide(array) {
  const oddNumbers = [];
  const evenNumbers = [];

  array.forEach(number => {
    if (number % 2 === 0) {
      oddNumbers.push(number);
    } else {
      evenNumbers.push(number);
    }
  });
  
  return {
    odd: oddNumbers,
    even: evenNumbers
  };
}
  1. Create a function that verifies how many times a number is repeated within an array
function checkRepeated(number, array) {
  let ocurrences = 0;
  array.forEach(index => index === number && ocurrences++);
  
  return `Number ${number} is repeated ${ocurrences} times`;
}
  1. Create a function that displays the missing value of an array.
function getMissingValue(array) {
  for (const [index, number] of array.entries()) {
		if (index !== number) {
			console.log(`The number ${index} is missing`);
      break;
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment