본문 바로가기

분류 전체보기

(157)
[javascript] 프로그래머스/예산 12345678910111213141516171819202122232425//최대한 많은 부서의 물품을 구매하는 것이 목적..function solution(d, budget) { var answer = 0; let arrSort = d.sort(function(a,b){ return a - b; }) ; let sum = 0; let i; for( i=0;ibudget){ //커지면 리턴.. answer = i; break; } else if(sum===budget){ //예산의 금액과 살려는 구매 물품의 합이 같을 경우. answer = i+1; break; } } if(i===arrSort.length){ //for문을 다 통과했을경우.. 예산이 높아서 그냥 다살수 있는경우 answer = arrS..
[javascript] 프로그래머스/직사각형 별찍기 1 2 3 4 5 6 process.stdin.setEncoding('utf8'); process.stdin.on('data', data => { const n = data.split(" "); const a = Number(n[0]), b = Number(n[1]); console.log(Array(b).fill((Array(a).fill("*").join(''))).join("\n")) }); Colored by Color Scripter cs Array(n) n개의길이를 가진 배열을 만드는 메소드 Array(n).fill(x) n개의 길이를 가진 배열을 x의 값으로 채워진 배열을 만드는 메소드 Array(n).join(x) 하나의 문자열로 만든다 배열사이의 간격을 x라는 문자로 채운다.
[javascript] 프로그래머스/x만큼 간격이 있는 n개의 숫자 처음 내가 한방법.. 1 2 3 4 5 6 7 8 9 10 11 //정수 x와 자연수 n을 입력 받는다 //정수 x부터 시작해 x씩 증가한 숫자를 n개 지니는 배열을 리턴.. function solution(x, n) { var answer = []; let put = x; for(let i=0;i n개 만큼의 수가 를어가는 배열을 만든다 단 값은 empty 로 들어감 // fill(x) => x으로진 값이 채워져 있는 배열로 만든다 [x,x,x,x,x,x,x,x,....] => n개 만큼 // mdn map 참고 .. index는 말그대로 인덱스 몇번쨰 인지 .. value 배열 각 인덱스의 값을 의미한다. 배열의 값 변경.. return Array(n).fill(x).map((value, index)..
[javascript] 프로그래머스/ 핸드폰 번호 가리기 1 2 3 4 5 6 7 8 9 10 11 12 13 //뒤에서 4자리만 그대로 ,, function solution(phone_number) { var answer = ''; for(let i=0;i=phone_number.length-4){ answer += phone_number[i]; } else{ answer +="*"; } } return answer; } Colored by Color Scripter cs 정규표현식 1 2 3 4 5 //뒤에서 4자리만 그대로 ,, function solution(phone_number) { var answer = phone_number.replace(/\d(?=\d{4})/g,"*"); return answer; } Colored by Color Scrip..
[javascript] 프로그래머스/제일 작은 수 제거 1 2 3 4 5 6 7 8 9 10 11 12 function solution(arr) { var answer = []; if(arr.length===1){ //값이 하나일땐 제거하면 무조건 없으므로 answer.push(-1); return answer; } let min = Math.min.apply(null,arr); //가장 작은값 가져오는 방법.. answer = arr.filter(function(element){ return element!==min }); return answer; } Colored by Color Scripter cs apply를 이용해 배열의 가장 작은 값 또는 큰값 가져오는 법을 알게되었다. apply() 메서드는 주어진 this 값과 배열 (또는 유사 배열 객체) ..
[javascript] 프로그래머스/정수 제곱근 판별 12345678//양의정수 x의 제곱이면 x+1의 제곱을 리턴... 아니면 -1리턴.function solution(n) { let sqrt = Math.sqrt(n); if(sqrt%1!==0) { return -1; }//나머지가 있을떈... 없다는것 return Math.pow(sqrt+1,2);}Colored by Color Scriptercs
[javascript] 프로그래머스/ 자연수 뒤집어 배열로 만들기 1 2 3 4 5 6 7 8 function solution(n) { var answer = []; let newArr = String(n).split("").reverse(); answer= newArr.map(function(element){ return Number(element); }) return answer; } Colored by Color Scripter cs 배열 뒤집는것 reserve 사용
[javascript] 프로그래머스/자릿수 더하기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 function solution(n) { let num = n; let sum = 0; while(true){ if(num