let players = ["mumu", "soe", "poe", "kai", "mine"];
let callings = ["kai", "kai", "mine", "mine"];
function solution(players, callings) {
// Map(0) {} 맵 객체 만들기
const hash = new Map();
// key => name, vlaue => index 로 해쉬 객체 초기화
// Map(5) { 'mumu' => 0, 'soe' => 1, 'poe' => 2, 'kai' => 3, 'mine' => 4 }
players.forEach((name, index) => {
hash.set(name, index);
});
//callings 변수 반복문으로 배열 값들 추출
callings.forEach((name) => {
// callings 의 kai, mine 두개의 index 값을 map객체에 들어있는 index 값에서 찾기
// 3 3 4 4 (map 객체에서 kai는 3번 인덱스 mine은 4번 인덱스이다)
const currIdx = hash.get(name);
// 앞지름에 대비해서 callings에 담긴 선수들의 이름의 앞 선수들을 추출 해놓음
// poe poe kai kai (각각 kai의 앞선수 mine의 앞선수이다.)
const front = players[currIdx - 1];
// 앞사람과 index 바꾸기
[players[currIdx], players[currIdx - 1]] = [
players[currIdx - 1],
players[currIdx],
];
// hash 객체 초기화로 순서 바꾼 사람들의 index를 변경해준다
// 뒤에 있다가 앞지른 사람은 index가 -1
// 앞에 있다가 역전 당한사람 index는 +1
hash.set(name, hash.get(name) - 1);
hash.set(front, hash.get(name) + 1);
});
return players;
}