목록JavaScript/프로그래머스 문제풀이 (4)
코로 넘어져도 헤딩만 하면 그만
💡영어가 싫어요function solution(numbers) { const dic = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; let answer = ""; let currentWord = ""; for (let i = 0; i answer을 담는 변수와 현재 단어를 담는 변수를 만들어, dic의 index+1만큼 더하는 방식을 적용했다.객체로 담은 뒤 replace메서드를 콜백 함수와 함께 사용하는 방식이 있어 적어둔다. ✨ replace 1번째 인자는 정규표현식이 올수 있고 2번째 인자는 콜백이 올 수 있다. function solution(numbers) { co..
💡모음 제거하기function solution(my_string) { return my_string.split('').filter(char => !'aeiou'.includes(char)).join('');}!'aeiou'를 통해 char이 모음 리스트에 포함되지 않는 경우에만 filter한 뒤 join으로 문자열화 시켜 묶어준다. 아래와 같이 replace()와 정규 표현식으로 사용할 수도 있다. tip: replace() 메서드는 pattern의 단일, 일부 혹은 모든 일치 항목이 replacement로 대치된 새 문자열을 반환한다.function solution(my_string) { return my_string.replace(/[aeiou]/g, '');} 💡숨어있는 숫자의 덧셈f..
💡오름차순, 내림차순 sort()로 정렬하기function solution(array) { const resortList = array.sort((a, b)=>a-b); const mid_num = Math.trunc(array.length / 2); const answer = resortList[mid_num]; return answer;}어제 푼 문제에서 나온 Math.trunc()로 소수점 이하의 수를 버리는 방식을 취했다. floor을 썼어도 괜찮았을 것 같다.sort()는 기본적으로 문자를 UTF-16 코드 유닛 값을 기준으로 정렬한다. 따라서 숫자 정렬을 제대로 하기 위해 커스터마이징이 필요하다. 이번에는 a-b를 사용해서 오름차순으로 정렬해주었다.또한, sort()의 경우 원..
💡Math.trunc()Math.floor()은 소수점을 내림하지만, Math.trunc()는 소수점 이하를 다 버린다. Math.floor(23.333) //23Math.floor(-23.333) //-24Math.trunc(23.333) //23Math.trunc(-23.333) //-23 💡 삼항 연산자를 사용하여 값 비교하기function solution(num1, num2) { var answer = num1 === num2 ? 1 : -1; return answer;}나는 if else문으로 풀었던 문제인데, 삼항 연산자로 깔끔하게 한 줄로 표시할 수 있다. 💡최대공약수 구하기function fnGCD(a, b){ return (a%b)? fnGCD(b, a%b) :..