Skip to content

Commit 172c35e

Browse files
committed
I_hate_english / 기초
1 parent ab5c5c4 commit 172c35e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tkddbs587/Stack/I_hate_english.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function solution(numbers) {
2+
let numArr = [
3+
"zero",
4+
"one",
5+
"two",
6+
"three",
7+
"four",
8+
"five",
9+
"six",
10+
"seven",
11+
"eight",
12+
"nine",
13+
]; // 0 ~ 9 배열 생성
14+
let strNum = ""; // 숫자를 문자열로 표현한 numbers를 인덱스 순서대로 담을 변수
15+
let result = ""; // 숫자 단어에 해당하는 숫자를 담을 변수
16+
for (let i = 0; i < numbers.length; i++) {
17+
strNum += numbers[i]; // 변수에 numbers 인덱스 순서대로 담기
18+
19+
if (numArr.includes(strNum)) {
20+
// 차례대로 담다가 배열에 해당하는 숫자가 있으면
21+
result += numArr.indexOf(strNum); // 해당 숫자 인덱스를 변수에 추가
22+
strNum = ""; // numbers에 있는 다음 숫자 단어를 더해주기 위해 초기화
23+
}
24+
}
25+
return Number(result); // 문자열로 돼있는 숫자 변수를 number 타입으로 변환 후 결과 반환
26+
}

0 commit comments

Comments
 (0)