Restore a Coherent Narrative from an Array of Story Fragments - Restore a Coherent Narrative from an Array of Story Fragments

Tell us what’s happening:

Test 22 is failing. i dont know what to do. it says “dedupedFragments should have no duplicate id values”. please help

Your code so far

const shuffledFragments = [
  { id: 15, text: "and, after a time, passed the place where the Hare was sleeping." },
  { id: 12, text: "he lay down beside the course to take a nap" },
  ,
  { id: 11, text: "and to make the Tortoise feel very deeply how ridiculous it was for him to try a race with a Hare," },
  { id: 7, text: "but for the fun of the thing he agreed." },
  { id: 19, text: "The Hare now ran his swiftest," },
  ,
  { id: 1, text: "A Hare was making fun of the Tortoise one day for being so slow." },
  { id: 14, text: "The Tortoise meanwhile kept going slowly but steadily," },
  { id: 9, text: "marked the distance and started the runners off." },
  ,
  { id: 5, text: "I'll run you a race and prove it.\"" },
  { id: 17, text: "and when at last he did wake up," },
  { id: 2, text: '"Do you ever get anywhere?" he asked with a mocking laugh.' },
  { id: 12, text: "he lay down beside the course to take a nap" },
  ,
  { id: 8, text: "So the Fox, who had consented to act as judge," },
  { id: 20, text: "but he could not overtake the Tortoise in time." },
  { id: 5, text: "I'll run you a race and prove it.\"" },
  { id: 6, text: "The Hare was much amused at the idea of running a race with the Tortoise," },
  ,
  { id: 13, text: "until the Tortoise should catch up." },
  { id: 10, text: "The Hare was soon far out of sight," },
  { id: 12, text: "he lay down beside the course to take a nap" },
  { id: 18, text: "the Tortoise was near the goal." },
];

function compactFragments(arr) {
  let result = [];
  let removed = false;
  for(let i = 0; i < arr.length; i++) {
    if(arr[i] === undefined) {
      removed = true
    } else {
      result.push(arr[i])
    }

   if(removed) {
      console.log("[COMPACTED]")
    }
  }
   return result
}

let compactedShuffledFragments = compactFragments(shuffledFragments) 

function sortFragments(arr) {
 let sortedArr = [...arr]
 
 for(let i = 0; i < sortedArr.length; i++) {
  for(let j = 0; j < sortedArr.length - 1 - i; j++) {
    if(sortedArr[j].id > sortedArr[j + 1].id) {
      let temp = sortedArr[j]
      sortedArr[j] = sortedArr[j + 1]
      sortedArr[j + 1] = temp 
    }
  }
 }
 return sortedArr
}

let sortedFragments = sortFragments(compactedShuffledFragments)

function dedupeFragments(arr) {
  let newArr = [...arr]
  let dedupe = [newArr.shift()]
  let countDedupes = 0
  let duplicates = []

  for(let i = 0; i < newArr.length; i++)  {
    let prevFrag = dedupe.at(-1)
   if(newArr[i].id === prevFrag.id) {
     countDedupes++
    duplicates.push(newArr[i])
   } else {
    dedupe.push(newArr[i])
   }
  }
  

  for(let dup of duplicates) {
    console.log("[DEDUPED]")
  }
  return dedupe;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36

Challenge Information:

Restore a Coherent Narrative from an Array of Story Fragments - Restore a Coherent Narrative from an Array of Story Fragments

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-story-fragment-restoration/697fc395f3d85f14b05cd9ca.md at main · freeCodeCamp/freeCodeCamp · GitHub

it does not look like the variable exist yet, so any test about the variable fail by default

User story 7

You should declare a variable named dedupedFragments and assign it the result of calling dedupeFragments with the sortedFragments array.

oh sorry. thank you so muchhh. i was stuck. didnt read ahead.