Pages

help

 const tags = [

  "section",
  "article",
  "h1",
  "h2",
  "p",
  "article",
  "p",
  "p",
  "span",
  "span",
  "span",
];

//find unique tags
const uniqueTags = [...new Set(tags)];
console.log("Unique Tags using Set:", uniqueTags);

//find unique tags whitout Set
const uniqueTagsWithoutSet = tags.filter(
  (tag, index) => tags.indexOf(tag) === index
);
console.log("Unique Tags without Set:", uniqueTagsWithoutSet);

//find unique tags using reduce
const uniqueTagsUsingReduce = tags.reduce((acc, tag) => {
  if (!acc.includes(tag)) {
    acc.push(tag);
  }
  return acc;
}, []);
console.log("Unique Tags using reduce:", uniqueTagsUsingReduce);

// find occurrences of each tag using reduce
const tagOccurrences = tags.reduce((acc, tag) => {
  acc[tag] = (acc[tag] || 0) + 1;
  return acc;
}, {});
console.log("Tag Occurrences using reduce:", tagOccurrences);

// find the most frequent tag using reduce

const mostFrequentTag = Object.entries(tagOccurrences).reduce(
  (max, current) => (current[1] > max[1] ? current : max),
  ["", 0]
);
console.log("Most Frequent Tag using reduce:", mostFrequentTag);

const listOfRandomTags = ["section", "footer"];

// Check if all tags in listOfRandomTags are present in uniqueTagsUsingReduce
const allTagsPresent = listOfRandomTags.every((tag) =>
  uniqueTagsUsingReduce.includes(tag)
);
console.log("All tags present in uniqueTagsUsingReduce:", allTagsPresent);

// Check if any tag in listOfRandomTags is present in uniqueTagsUsingReduce
const anyTagPresent = listOfRandomTags.some((tag) =>
  uniqueTagsUsingReduce.includes(tag)
);

console.log("Any tag present in uniqueTagsUsingReduce:", anyTagPresent);

// groupe tagOccurrences by two values sup of occurences = 2 and less than or equal to 2

const groupedTagOccurrences = Object.entries(tagOccurrences).reduce(
  (acc, [tag, count]) => {
    if (count > 2) {
      acc.moreThanTwo.push({ tag, count });
    } else {
      acc.twoOrLess.push({ tag, count });
    }
    return acc;
  },
  { moreThanTwo: [], twoOrLess: [] }
);
console.log("Grouped Tag Occurrences:", groupedTagOccurrences);