[알고리즘] 2024-02-29(목)

dev-riverkim·2024년 3월 13일
0

8 kyu - Abbreviate a Two Word Name

Write a function to convert a name into initials. This kata strictly takes two words with one space in between them.

The output should be two capital letters with a dot separating them.

It should look like this:

Sam Harris => S.H

patrick feeney => P.F

Solution

function abbrevName(name){
  return name.split(" ").map(item =>item[0].toUpperCase()).join('.')
}
function abbrevName(name){

  var nameArray = name.split(" ");
  return (nameArray[0][0] + "." + nameArray[1][0]).toUpperCase();
}
function abbrevName(name){
  return name.split(' ').map(x => x.substr(0, 1).toUpperCase()).join('.');
}
const abbrevName = name => name.match(/\b\w/g).join('.').toUpperCase()
function abbrevName(name){
    return name[0].toUpperCase() + "." + name[name.indexOf(" ")+1].toUpperCase();
}

8 kyu - Convert a string to an array

Write a function to split a string and convert it into an array of words.

Examples (Input ==> Output):

"Robin Singh" ==> ["Robin", "Singh"]

"I love arrays they are my favorite" ==> ["I", "love", "arrays", "they", "are", "my", "favorite"]

Solution

function stringToArray(string){
	  return string.split(" ");
}

8 kyu - Find the smallest integer in the array

Given an array of integers your solution should find the smallest integer.

For example:

  • Given [34, 15, 88, 2] your solution will return 2
  • Given [34, -345, -1, 100] your solution will return 345

You can assume, for the purpose of this kata, that the supplied array will not be empty.

Solution

class SmallestIntegerFinder {
  findSmallestInt(args) {
    return Math.min(...args)
  }
}
class SmallestIntegerFinder {
  findSmallestInt(args) {
    return Math.min.apply(null, args);
  }
}
class SmallestIntegerFinder {
  findSmallestInt(args) {
    return args.sort((a,b)=>a-b)[0];
  }
}
class SmallestIntegerFinder {
  findSmallestInt(args) {
    return args.reduce(function(prev, curr, index, array) {
      return prev < curr ? prev : curr;
    });
  }
}
profile
dev-riverkim

0개의 댓글