I am designing a web app and the below code is supposed to find the most common HTML tag in a page’s source code, but it always returns ‘html’ instead of the correct tag. Why doesn’t this work as expected?
function mostCommonTag(url) {
const response = await fetch(url);
const html = await response.text();
const tags = [];
const regex = /<(w+)[^>]*>/g;
let match;
while ((match = regex.exec(html)) !== null) {
tags.push(match[1]);
}
const mostCommon = tags.reduce((a, b) => tags.filter(v => v === a).length > tags.filter(v => v === b).length ? a : b);
return mostCommon;
}
console.log(mostCommonTag('(link unavailable)'));
I have tried doing a lot of research on this but I’m still pretty stumped!
Any advice would be greatly appreciated!
P.S. please let me know if this is a duplicate- I had a look and couldn’t find one but this site has a vast number of questions!
security_paranoid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1