function solution(numbers, hand) {
var answer = '';
var keyPad = new Map([
['1',[0,0]],
['2',[0,1]],
['3',[0,2]],
['4',[1,0]],
['5',[1,1]],
['6',[1,2]],
['7',[2,0]],
['8',[2,1]],
['9',[2,2]],
['*',[3,0]],
['0',[3,1]],
['#',[3,2]],
]);
var lefthand = '*';
var righthand = '#';
for (const num of numbers)
{
if (num === 1 || num === 4 || num === 7)
{
answer += ('L');
lefthand = String(num);
}
else if (num === 3 || num === 6 || num === 9)
{
answer += ('R');
righthand = String(num);
}
else{
var leftToTarget = Math.abs(keyPad.get(String(num))[0]-keyPad.get(lefthand)[0]) + Math.abs(keyPad.get(String(num))[1]-keyPad.get(lefthand)[1]);
var rightToTarget = Math.abs(keyPad.get(String(num))[0]-keyPad.get(righthand)[0]) + Math.abs(keyPad.get(String(num))[1]-keyPad.get(righthand)[1]);
if (leftToTarget < rightToTarget)
{
answer += ('L');
lefthand = String(num);
}
else if (leftToTarget > rightToTarget)
{
answer += ('R');
righthand = String(num);
}
else
{
if (hand === 'left')
{
answer += 'L';
lefthand = String(num);
}
else
{
answer += 'R';
righthand = String(num);
}
}
}
}
return answer;
}
'๐ฏ CodingTest > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] (Javascript) ์คํจ์จ (0) | 2022.04.21 |
---|---|
[Programmers] (Javascript) ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ (0) | 2022.04.21 |
[Programmers] (Javascript) ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.04.21 |
[Programmers] (Javascript) ์ ๊ท์์ด๋ ์ถ์ฒ (0) | 2022.04.21 |
[Programmers] (Javascript) ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.04.21 |