사랑하애오
[프로그래머스 | 자바스크립트] 삼각형의 완성조건 (1)

솔루션 const solution = (sides) => { const max = Math.max(...sides); const answer = max a + c, 0) - max ? 1 : 2; console.log(answer); return answer; }; 솔루션 const solution = (sides) => { sides = sides.sort((a, b) => a - b); const answer = sides[0] + sides[1] > sides[2] ? 1 : 2; console.log(answer); return answer; };

[프로그래머스 | 자바스크립트] 가까운 수

솔루션 const solution = (array, n) => { const minDiff = Math.min(...array.map((v) => Math.abs(v - n))); console.log(minDiff); const answer = array .sort((a, b) => a - b) .find((a) => Math.abs(a - n) === minDiff); console.log(answer); return answer; }; Math 메서드의 min(최솟값)과 abs(절댓값)을 이용

[프로그래머스 | 자바스크립트] 369 게임

솔루션 const solution = (order) => { const str = [...("" + order)].filter( (v) => v === "3" || v === "6" || v === "9" ); const answer = str.length; console.log(answer); return answer; }; 솔루션 const solution = (order) => { const answer = [...order.toString().matchAll(/[3|6|9]/g)].length; console.log(answer); return answer; }; 정규식을 이용한 풀이 솔루션 const solution = (order) => { const set = new Set([3, 6, 9]..

[프로그래머스 | 자바스크립트] 암호 해독

솔루션 const solution = (cipher, code) => { const str = [...cipher]; const answer = str.reduce( (a, c, i) => ((i + 1) % code === 0 ? a + c : a), "" ); console.log(answer); return answer; }; 솔루션 const solution = (cipher, code) => { const answer = []; for (let i = code - 1; i < cipher.length; i += code) { answer.push(cipher[i]); } console.log(answer.join("")); return answer.join(""); }; 솔루션 const sol..

[프로그래머스 | 자바스크립트] 대문자와 소문자

솔루션 const solution = (my_string) => { const str = [...my_string]; console.log(str); const answer = str .map((v) => (v === v.toUpperCase() ? v.toLowerCase() : v.toUpperCase())) .join(""); console.log(answer); return answer; }; 솔루션 const solution = (my_string) => { let answer = ""; for (const str of my_string) { answer += str === str.toLowerCase() ? str.toUpperCase() : str.toLowerCase(); } console..

[프로그래머스 | 자바스크립트] 영어가 싫어요

솔루션 const num = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", ]; const solution = (numbers) => { for (let i = 0; i < num.length; i++) { numbers = numbers.split(num[i]).join(i); } const answer = Number(numbers); console.log(answer); return answer; };

[프로그래머스 | 자바스크립트] 인덱스 바꾸기

솔루션 const solution = (my_string, num1, num2) => { const str = [...my_string]; console.log(str); [str[num1], str[num2]] = [str[num2], str[num1]]; const answer = str.join(""); console.log(answer); return answer; }; 솔루션 const solution = (my_string, num1, num2) => { const str = []; let answer = ""; for (let i = 0; i < my_string.length; i++) { if (i === num1 || i === num2) { str.push(my_string[i]); }..

[프로그래머스 | 자바스크립트] 한 번만 등장한 문자

솔루션 const solution = (s) => { const count = [...s].reduce( (a, c) => (a[c] ? { ...a, [c]: a[c] + 1 } : { ...a, [c]: 1 }), {} ); console.log(count); const answer = Object.keys(count) .filter((key) => count[key] === 1) .sort((a, b) => (a b ? 1 : 0)) .join(""); console.log(answer); return answer; }; 솔루션 const solution = (s) => { let answer = []; for (const v of s) { if (s.indexOf(v) ..