프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다.
전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수, solution을 완성해주세요.
s는 길이 4 이상, 20이하인 문자열입니다.
11자리가 오든, 10자리가 오든, n자리가 오든 끝에 숫자 4개를 제외한 나머지 숫자는 '*'
로 가려야한다.
순서대로 for문을 돌려주면서 뒤에서 5번째까지(<length-4) 전까지 '*'
을 넣고, 뒤에서 4번째부터 숫자를 넣어주려고 했다.
function solution(phone_number) {
var answer = '';
let length = phone_number.length;
for(i=0; i<length; i++) {
if(i < length-4){
answer= answer+'*';
}
else {
answer = answer + phone_number[i];
}
}
return answer;
}
function hide_numbers(s){
var result = "*".repeat(s.length - 4) + s.slice(-4);
//함수를 완성해주세요
return result;
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
console.log("결과 : " + hide_numbers('01033334444'));
String.prototype.repeat()
: repeat() 메서드
는 문자열을 주어진 횟수만큼 반복해 붙인 새로운 문자열을 반환한다.
str.repeat(count);
;'abc'.repeat(0); // ''
'abc'.repeat(1); // 'abc'
'abc'.repeat(2); // 'abcabc'
String.prototype.slice()
: slice() 메소드는 문자열의 일부를 추출하면서 새로운 문자열을 반환한다.
str.slice(beginIndex[, endIndex])
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(0));
// expected output: "The quick brown fox jumps over the lazy dog."
console.log(str.slice(5));
// 5 ~ 끝
// expected output: "uick brown fox jumps over the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"
console.log(str.slice(-1));
// expected output: "."
console.log(str.slice(-4));
// -4 ~ 끝
// expected output: "dog."
console.log(str.slice(-9, -5));
// -9 ~ -5
// expected output: "lazy"
문제출처: https://programmers.co.kr/learn/courses/30/lessons/12948
출처:mdn
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.
1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.
각 수포자마다 정답 패턴이 있다. 문제 수만큼 패턴을 반복해서 채점을 해야한다. 예를 들어 1번 수포자의 경우 array = [1,2,3,4,5]
5개 숫자가 반복되는 패턴이다.
1번 숫자가 10개의 문제를 푼다고하면 입력되는 index값이
0-1-2-3-4-5-6-7-8-9가 아니라
0-1-2-3-4-0-1-2-3-4가 되어야 한다.
따라서 array[question.index]
가 아니라 array[question.index%array.length]
를 넣어줘야한다.
let stu1 = [1,2,3,4,5];
let stu2 = [2,1,2,3,2,4,2,5];
let stu3 = [3,3,1,1,2,2,4,4,5,5];
function solution(answers) {
let answer = [];
let total1 = answers.filter((a,i) => a === stu1[i%5]).length;
let total2 = answers.filter((a,i) => a === stu2[i%8]).length;
let total3 = answers.filter((a,i) => a === stu3[i%10]).length;
let max = Math.max(total1, total2, total3);
(total1 === max) && answer.push(1);
(total2 === max) && answer.push(2);
(total3 === max) && answer.push(3);
return answer;
}
solution([1,2,3,4,5]); // [1];
solution([1,3,2,4,2]); // [1,2,3];
function solution(answers) {
var answer = [];
var a1 = [1, 2, 3, 4, 5];
var a2 = [2, 1, 2, 3, 2, 4, 2, 5]
var a3 = [ 3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
var a1c = answers.filter((a,i)=> a === a1[i%a1.length]).length;
var a2c = answers.filter((a,i)=> a === a2[i%a2.length]).length;
var a3c = answers.filter((a,i)=> a === a3[i%a3.length]).length;
var max = Math.max(a1c,a2c,a3c);
if (a1c === max) {answer.push(1)};
if (a2c === max) {answer.push(2)};
if (a3c === max) {answer.push(3)};
return answer;
}
Math.max()
함수는 입력값으로 받은 0개 이상의 숫자 중 가장 큰 숫자를 반환합니다.
console.log(Math.max(1, 3, 2));
// expected output: 3
console.log(Math.max(-1, -3, -2));
// expected output: -1
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
// expected output: 3
문제출처: https://programmers.co.kr/learn/courses/30/lessons/42840
출처:mdn
배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.
예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면
array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
2에서 나온 배열의 3번째 숫자는 5입니다.
배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.
array의 2번째부터 5번째까지 자르는 것은 slice(1,5)
와 같고,
오름차순 정렬은 sort((a,b)=>(a-b))
,
배열의 3번째 숫자는 array[2]
이다.
따라서 커맨드를 다음과 같이 적용하고
array.slice(command[0]-1,command[1]).sort((a,b)=>a-b)[command[2]]
;
for문을 돌리려고 했다.
function solution(array, commands) {
var answer = [];
for (let i = 0; i<commands.length; i++) {
answer.push(array.slice((commands[i][0])-1,commands[i][1]).sort((a,b)=>a-b)[commands[i][2]-1]);
}
return answer;
}
solution([1, 5, 2, 6, 3, 7, 4],[[2, 5, 3], [4, 4, 1], [1, 7, 3]]); // [5,6,3];
function solution(array, commands) {
return commands.map(command => {
const [sPosition, ePosition, position] = command // 구조분해할당
const newArray = array
.filter((value, fIndex) => fIndex >= sPosition - 1 && fIndex <= ePosition - 1)
.sort((a,b) => a - b)
//index (sPosition-1) ~ (ePosition-1)
return newArray[position - 1]
})
}
[1,2,3,4,5].filter((value,fIndex)=> fIndex >= 0 && fIndex <= 4);
//[ 1, 2, 3, 4, 5 ] Index 0~4
문제출처: https://programmers.co.kr/learn/courses/30/lessons/42748
출처:mdn
서영님 몸이 몇개세요...?