[04.13.22] Coding test

Juyeon.it·2022년 4월 13일
0

Coding test

목록 보기
2/32

Highest and Lowest

Description

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.
Notes
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
Examples
highAndLow("1 9 3 4 -5"); // return "9 -5"

My answer

function highAndLow(numbers){
  let res = numbers.split(' ').map(num => parseInt(num)).sort(function(a, b) { return a - b });
  return `${res[res.length - 1].toString()} ${res[0]}`;
}

Other solutions

function highAndLow(numbers){
  numbers = numbers.split(' ').map(Number);
  return Math.max.apply(0, numbers) + ' ' + Math.min.apply(0, numbers);
}
function highAndLow(numbers){
  numbers = numbers.split(' ');
  return `${Math.max(...numbers)} ${Math.min(...numbers)}`;
}

Wrap up

sort function

array.sort([compareFunction])

Parameters

compareFunction(optional)

If compareFunction is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array.
If compareFunction is supplied,
compareFunction(a,b) return value > 0 -> sort b before a
compareFunction(a,b) return value < 0 -> sort a before b
compareFunction(a,b) return value === 0 -> keep original order of a and b

Examples

let stringArray = ['Blue', 'Humpback', 'Beluga'];
let numberArray = [40, 1, 5, 200];
let numericStringArray = ['80', '9', '700'];
let mixedNumericArray = ['80', '9', '700', 40, 1, 5, 200];

function compareNumbers(a, b) {
  return a - b;
}

stringArray.join(); // 'Blue,Humpback,Beluga'
stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']

numberArray.join(); // '40,1,5,200'
numberArray.sort(); // [1, 200, 40, 5]
numberArray.sort(compareNumbers); // [1, 5, 40, 200]

numericStringArray.join(); // '80,9,700'
numericStringArray.sort(); // ['700', '80', '9']
numericStringArray.sort(compareNumbers); // ['9', '80', '700']

mixedNumericArray.join(); // '80,9,700,40,1,5,200'
mixedNumericArray.sort(); // [1, 200, 40, 5, '700', '80', '9']
mixedNumericArray.sort(compareNumbers); // [1, 5, '9', 40, '80', 200, '700']

apply function

func.apply(thisArg, [argsArray])

Parameters

thisArg

The value of this provided for the call to func.

argsArray(optional)

An array-like object, specifying the arguments with which func should be called, or null or undefined if no arguments should be provided to the function.

Examples

const array = ['a', 'b'];
const elements = [0, 1, 2];
array.push(array, elements);
console.info(array); // ["a", "b", [0, 1, 2]]

// apply function
const array = ['a', 'b'];
const elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); // ["a", "b", 0, 1, 2]
// min/max number in an array
const numbers = [5, 6, 2, 3, 7];

// using Math.min/Math.max apply
let max = Math.max.apply(null, numbers);
// This about equal to Math.max(numbers[0], ...)
// or Math.max(5, 6, ...)

let min = Math.min.apply(null, numbers);

// vs. simple loop based algorithm
max = -Infinity, min = +Infinity;

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] > max) {
    max = numbers[i];
  }
  if (numbers[i] < min) {
    min = numbers[i];
  }
}

source: mdn web docs/Array.prototype.sort(), mdn web docs/Function.prototype.apply()

Friend or Foe?

Description

Make a program that filters a list of strings and returns a list with only your friends name in it.
If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not...
Note: keep the original order of the names in the output.

My answer

function friend(friends){
  return friends.filter(friend => friend.length === 4);
}

Stop gninnipS My sdroW!

Description

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 (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Examples:
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"

My answer

function spinWords(string){
  let result = string.split(' ').map(function(word){
    if (word.length >= 5) {
      return word.split("").reverse().join('');
    } else { return word }
  });
  return result.join(' ');
}

Other solutions

function spinWords(words){
  return words.split(' ').map(function (word) {
    return (word.length > 4) ? word.split('').reverse().join('') : word;
  }).join(' ');
}
function spinWords(str){
  return str.split(' ').map( w => w.length<5 ? w : w.split('').reverse().join('') ).join(' ');
}

0개의 댓글

관련 채용 정보