정규식을 사용해 풀려다 화딱지 나서 split으로 난도질 했다.
function solution(word, pages) {
word = word.toLowerCase();
const pageInfos = [];
const scores = [];
const linkScores = Array(pages.length).fill(0);
for(let i = 0; i < pages.length; i++) {
const page = pages[i].toLowerCase();
const url = page.split('<meta property="og:url" content="')[1].split('"/>')[0];
const body = page.split('<body>')[1].split('</body>')[0];
const externalLinks = body.split('<a href="').slice(1).map(v => v.split('">')[0]);
const refinedTexts = body.replace(/[^a-z ]/g, ' ').split(' ').filter(v => v === word);
const baseScore = refinedTexts.length;
pageInfos.push({ i, baseScore, url, externalLinks });
scores.push(baseScore);
}
for(const { i, baseScore, externalLinks } of pageInfos) {
for(const link of externalLinks) {
const linkedPage = pageInfos.find(page => page.url === link);
if(linkedPage) linkScores[linkedPage.i] += baseScore / externalLinks.length;
}
}
const totalScores = scores.map((score, i) => score + linkScores[i]);
const maxScore = Math.max(...totalScores);
return totalScores.indexOf(maxScore);
}