프로그래머스 - 로또의 최고 순위와 최저 순위 [80]
https://programmers.co.kr/learn/courses/30/lessons/77484?language=javascript
오늘부터 js로 코테 준비를 하기로 결심하였다!
그래서 전에 풀었던 문제들도 js로 풀고
프로그래머스가 js 지원이 잘 되어있어서 백준 말고 프로그래머스 부터 풀기로 했다
파이팅!!
문제는 링크 참조
생각
js 를 많이 써보지 않아서 includes() 메소드가 시간복잡도에 걸릴까 걱정했는데 그렇지는 않았다.
휴…
찾아본 사항
-
Array.prototype.includes()
includes()메서드는 배열이 특정 요소를 포함하고 있는지 판별합니다.사용법
const array1 = [1, 2, 3]; console.log(array1.includes(2)); // expected output: true const pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // expected output: true console.log(pets.includes('at')); // expected output: false -
한 줄에 여러 변수를 정의하는 방법 js
http://daplus.net/javascript-javascript-%ED%95%9C-%EC%A4%84%EC%97%90-%EC%97%AC%EB%9F%AC-%EB%B3%80%EC%88%98%EB%A5%BC-%EC%A0%95%EC%9D%98%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95%EC%9D%80-%EB%AC%B4%EC%97%87%EC%9E%85/
// include 쓰는 법 찾기!
function solution(lottos, win_nums) {
const rank = [6, 6, 5, 4, 3, 2, 1]; // 6까지 존재이므로 length == 7
let [cnt, zeroCnt] = [0, 0]; //초기값
for (const x of lottos) {
if (x == 0) {
zeroCnt++;
} else if (win_nums.includes(x)) {
cnt++;
}
}
console.log(cnt, zeroCnt);
return [rank[cnt + zeroCnt], rank[cnt]];
}
lottos = [45, 4, 35, 20, 3, 9];
win_nums = [20, 9, 3, 45, 4, 35];
console.log(solution(lottos, win_nums));