728x90
๐ 19 - ๊ฐ์ ๋ฐ์ ๋ณด
๊ฒฝ์ฐ์์๋ฅผ ์ ๋ฐ์ ธ์ ํ์ด์ผํ๋๋ฐ, if
๋ฌธ์ A
๊ฐ ์ด๊ธด๊ฒฝ์ฐ, else - if
๋ฌธ์ ๋น๊ธด๊ฒฝ์ฐ, else
๋ฌธ(B
๊ฐ ์ด๊ธด๊ฒฝ์ฐ)์์๋ก ์์ฑํ๋ฉด ์ฝ๋์ ์์ ์ค์ผ ์ ์๋ค. ๋๋ฒ์งธ ์ฝ๋๋ if - else if - else
๋ฅผ ์ผํญ์ฐ์ฐ์๋ก ์์ถํด์ ์์ฑํ๋ค.
let n = 5;
let a = [2, 3, 3, 1, 3];
let b = [1, 1, 2, 2, 3];
console.log(solution(n, a, b));
// &&
function solution(n, a, b) {
let scissor = 1, rock = 2, paper = 3;
let ans = "";
for (i = 0; i < n; i++) {
if (
(a[i] === scissor && b[i] === paper) ||
(a[i] === rock && b[i] === scissor) ||
(a[i] === paper && b[i] === rock)
) {
ans += "A ";
} else if (a[i] === b[i]) {
ans += "D ";
} else {
ans += "B ";
}
}
return ans;
}
// ์ผํญ์ฐ์ฐ์
function solution(n, a, b) {
let scissor = 1,
rock = 2;
paper = 3;
let ans = "";
for (i = 0; i < n; i++) {
a[i] === b[i]
? (ans += "D ")
: a[i] === scissor && b[i] === paper
? (ans += "A ")
: a[i] === rock && b[i] === scissor
? (ans += "A ")
: a[i] === paper && b[i] === rock
? (ans += "A ")
: (ans += "B ");
}
return ans;
}
// ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋์ดํ ์ฝ๋
function solution(n, a, b) {
let scissor = 1, rock = 2
let ans = "";
for (i = 0; i < n; i++) {
if (a[i] === scissor) {
if (b[i] === scissor) {
ans += "D";
} else if (b[i] === rock) {
ans += "B";
} else {
ans += "A";
}
} else if (a[i] === rock) {
if (b[i] === scissor) {
ans += "A";
} else if (b[i] === rock) {
ans += "D";
} else {
ans += "B";
}
} else {
if (b[i] === scissor) {
ans += "B";
} else if (b[i] === rock) {
ans += "A";
} else {
ans += "D";
}
}
}
return ans;
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 21 - ๋ฑ์ ๊ตฌํ๊ธฐ (0) | 2021.08.13 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 20 - ์ ์ ๊ณ์ฐ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 18 - ๋ณด์ด๋ ํ์ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 17 - ํฐ ์ ์ถ๋ ฅํ๊ธฐ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 16 - ์ค๋ณต ๋จ์ด ์ ๊ฑฐ (0) | 2021.08.12 |
๋๊ธ