[04.14.22] Coding test

Juyeon.it·2022년 4월 14일
0

Coding test

목록 보기
3/32

Array.diff

Description

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b keeping their order.
If a value is present in b, all of its occurrences must be removed from the other.

My answer

function arrayDiff(a, b) {
  return a.filter(element => b.indexOf(element) === -1)
}

Other solutions

function array_diff(a, b) {
  return a.filter(e => !b.includes(e));
}
function array_diff(a, b) {
  b = new Set(b)
  return a.filter(v => !b.has(v))
}

Who likes it?

Description

You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement the function which takes an array containing the names of people that like an item. It must return the display text.
Note: For 4 or more names, the number in "and 2 others" simply increases.

My answer

function likes(names) {
  let result;
  if (names.length === 0) {
    result = 'no one likes';
  } else if (names.length === 1) {
    result = `${names[0]} likes`;
  } else if (names.length === 2) {
    result = `${names[0]} and ${names[1]} like`;
  } else if (names.length === 3) {
    result = `${names[0]}, ${names[1]} and ${names[2]} like`;
  } else if (names.length > 3) {
    let num = names.length - 2;
    result = `${names[0]}, ${names[1]} and ${num} others like`;
  }
  
  return `${result} this`;
}

Other solutions

function likes(names) {
  names = names || [];
  switch(names.length){
    case 0: return 'no one likes this'; break;
    case 1: return names[0] + ' likes this'; break;
    case 2: return names[0] + ' and ' + names[1] + ' like this'; break;
    case 3: return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'; break;
    default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this';
  }
}
function likes(names) {
  return {
    0: 'no one likes this',
    1: `${names[0]} likes this`, 
    2: `${names[0]} and ${names[1]} like this`, 
    3: `${names[0]}, ${names[1]} and ${names[2]} like this`, 
    4: `${names[0]}, ${names[1]} and ${names.length - 2} others like this`, 
  }[Math.min(4, names.length)]
}

Find The Parity Outlier

Description

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.

My answer

function findOutlier(integers){
  let remains = integers.map(integer => integer % 2 == 0)
  let trueOrFalse = (remains.filter(e => e === true).length === 1) ? true : false;
  
  return integers[remains.indexOf(trueOrFalse)];
}

Other solutions

function findOutlier(int){
  var even = int.filter(a=>a%2==0);
  var odd = int.filter(a=>a%2!==0);
  return even.length==1? even[0] : odd[0];
}

0개의 댓글

관련 채용 정보