<풀이 코드>
const student = prompt().split(' ');
const votes = {};
var count;
for(var n in student){
votes[student[n]] = count;
}
for(var i in Object.keys(votes)){
count = 0;
for(var j in student){
if(Object.keys(votes)[i]==student[j]){
count += 1;
}
}
votes[Object.keys(votes)[i]] = count;
}
const max_count = Math.max(...Object.values(votes));
const banjang = Object.keys(votes).find(key => votes[key] == max_count);
console.log(`${banjang}(이)가 총 ${max_count}표로 반장이 되었습니다`);
=> 내가 푼거랑 답안 코드랑 좀 달라서 답안 코드도 첨부해놓음
답안에서는 foreach를 사용
<답안 코드>
const array = prompt('이름을 입력해주세요.').split(' ');
let result = {};
let winner = "";
for(let index in array){
let val = array[index];
result[val] = result[val] === undefined ? 1 : result[val] = result[val] + 1;
}
winner = Object.keys(result).reduce(function(a, b){
return result[a] > result[b] ? a : b
});
console.log(`${winner}(이)가 총 ${result[winner]}표로 반장이 되었습니다.`);