Write a function that removes the spaces from the string, then return the resultant string. Examples: Input -> Output "8 j 8 mBliB8g imjB8B8 jl B" -> "8j8mBliB8gimjB8B8jlB" "8 8 Bi fk8h B 8 BB8B B B B888 c hl8 BhB fd" -> "88Bifk8hB8BB8BBBB888chl8BhBfd" "8aaaaa dddd r " -> "8aaaaaddddr"
문제 풀이
function noSpace(x){
let deleteSpace = x.replaceAll(" ","");
return deleteSpace;
}
변수를 선언해서 replaceAll 메소드로 공백을 빈문자열으로 바꿔주는 코드 작성
코드 비교
function noSpace(x){return x.split(' ').join('')}
split() 메소드를 이용해서 공백을 기준으로 문자열을 분리하고,join('')으로 분리된 문자열을 공백 없이 합칩니다. 이 과정에서 문자열의 공백은 모두 제거된다.
문제- Codewars_Find the smallest integer in the array (8kyu)
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.
문제 풀이
class SmallestIntegerFinder {
findSmallestInt(args) {
return Math.min(...args);
}
}
배열의 최소값을 구하는 것이기 때문에 sread operator을 이용해서 배열을 꺼내고, Math.min() 메소드를 사용해 최소값을 찾는 코드 작성
코드 비교
class SmallestIntegerFinder {
findSmallestInt(args) {
return Math.min.apply(null, args);
}
}
위 코드에서는 Math.min() 메소드에 배열의 원소들을 풀어서 전달하기 위해 apply() 메소드를 활용했다. apply() 메소드는 파라미터로, 함수에서 사용할 this객체와 호출하는 함수로 전달할 파라미터를 입력받는다. 이 때 apply() 메소드의 2번째 파라미터(호출하는 함수로 전달할 파라미터)는 배열 형태로 입력한다.
위 예제를 보면, apply() 메소드의 첫번째 파라미터로는 Math.min() 함수 내부에서 사용할 this객체를 전달해야 하는데, 여기서는 따로 this객체를 지정해 줄 필요가 없으므로 null을 전달되었다.
apply() 메소드의 두번째 파라미터로는Math.min() 함수로 전달할 파라미터를 배열 형태로 넣어주면 되는데, Math.min() 함수에 전달할 파라미터를 배열 형태로 만들어 전달(arr)된 코드