선수들의 이름이 1등부터 현재 등수 순서대로 담긴 문자열 배열 players와 해설진이 부른 이름을 담은 문자열 배열 callings가 매개변수로 주어질 때, 경주가 끝났을 때 선수들의 이름을 1등부터 등수 순서대로 배열에 담아 return 하는 solution 함수를 완성해주세요.
players | callings | result |
---|---|---|
["mumu", "soe", "poe", "kai", "mine"] | ["kai", "kai", "mine", "mine"] | ["mumu", "kai", "mine", "soe", "poe"] |
function solution(players, callings) {
let playersLength = players.length;
let callingsLength = callings.length;
for(let i = 0; i < callingsLength; i++){
for(let j = 0; j < playersLength; j++){
if(players[j] === callings[i]){
let front = players[j-1];
players[j-1] = players[j];
players[j] = front;
}
}
}
return players;
}
function solution(players, callings) {
let answer = [];
let playersLength = players.length;
let callingsLength = callings.length;
let playerRank = {};
let rankPlayer = {};
for(let i = 0; i < playersLength; i++){
playerRank[players[i]] = i + 1;
rankPlayer[i + 1] = players[i];
}
for(let i = 0; i < callingsLength; i++){
let curRank = parseInt(playerRank[callings[i]]);
let curPlayer = rankPlayer[curRank];
let nextRank = curRank-1;
let nextPlayer = rankPlayer[nextRank];
playerRank[curPlayer] = nextRank;
playerRank[nextPlayer] = curRank;
rankPlayer[curRank] = nextPlayer;
rankPlayer[nextRank] = curPlayer;
}
return Object.values(rankPlayer);
}
store.setState({
movieList: data.results,
searchValue: $searchInput.value,
page: 1,
});
console.log(store.state.searchValue) // 아무것도 뜨지 않는다
setState(newState) {
for (const [key, value] of Object.entries(newState)) {
if (typeof this.state[key] !== "string") {
if (!this.state[key]) continue;
}
this.state[key] = value;
}
}