Last active
August 24, 2021 05:01
-
-
Save emayom/0b2ed44f371a75cf67cea69e1e955a8a to your computer and use it in GitHub Desktop.
[프로그래머스] 모의고사
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// solution 1 | |
function solution(answers) { | |
const submit = ["12345", "21232425", "3311224455"]; | |
function cnt(submit) { | |
const A_LENGTH = answers.length; | |
const S_LENGTH = submit.length; | |
if(A_LENGTH > S_LENGTH){ | |
submit = submit.repeat(Math.ceil(A_LENGTH/S_LENGTH)); | |
} | |
submit = submit.substring(0, A_LENGTH) | |
.split(''); | |
let cnt = 0; | |
submit.forEach((el, index) => { | |
if(el == answers[index]){ | |
cnt++; | |
} | |
}); | |
return cnt; | |
} | |
let temp = [cnt(submit[0]), cnt(submit[1]), cnt(submit[2])]; | |
let answer = []; | |
const MAX = Math.max(...temp); | |
temp.forEach((el, index) => { | |
if(el == MAX) | |
answer.push(index + 1); | |
}); | |
return answer; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// solution 2 | |
function solution(answers) { | |
const submit = ["12345", "21232425", "3311224455"]; | |
const temp = []; | |
for(let i of submit){ | |
const LENGTH = i.length; | |
let x = answers.filter((el, index) => el == i[index%LENGTH]) | |
.length; | |
temp.push(x); | |
} | |
const MAX = Math.max(...temp); | |
const answer = []; | |
temp.forEach((el, index) => { | |
if(el == MAX) | |
answer.push(index + 1); | |
}) | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment