문제
끝말잇기
문제풀이
function solution(n, words) {
var answer = [];
var book = [];
var firstWord = words[0];
var wordArr = [...firstWord];
var lastKeyword = wordArr[wordArr.length - 1];
book.push(firstWord);
for (var i = 1; i < words.length; i++) {
var word = words[i];
var wordDiffArr = [...word];
var firstKeyword = wordDiffArr[0];
var lastWord = wordDiffArr[wordDiffArr.length - 1];
if (book.indexOf(word) != -1) break;
if (lastKeyword != firstKeyword) break;
lastKeyword = lastWord;
book.push(word);
}
if (words.length != book.length) {
var person = book.length % n + 1;
var games = parseInt(book.length / n ) + 1;
return [person, games];
}
else return [0,0];
return answer;
}