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:

my sortedFragments function keeps the failing the test case 15: “sortedFragments should have each element’s id less than or equal to the next element’s id.”. It seems to pass every test case that i try.

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." },
];

const compactFragments = fragments => {
  const compacted = [];

  for (const fragment of fragments) {
    if (fragment) {
      compacted.push(fragment);
    } else {
      console.log('[COMPACTED]: Removed undefined element');
    }
  }

  return compacted;
};

const compactedShuffledFragments = compactFragments(shuffledFragments);

const sortFragments = fragments => {
  const sorted = [];

  for (const fragment of fragments) {
    if (sorted.length === 0) sorted.push(fragment);
    else {
      if (fragment.id >= sorted[sorted.length - 1].id){
        sorted.push(fragment);
      } else {
        for (let i = 0; i < sorted.length; i++) {
          if (fragment.id < sorted[i].id) {
            sorted.splice(i, 0, fragment);
            break;
          }
        }
      }
    }
  }

  return sorted;
};

console.log(sortFragments([{ id: 30, text: "c" }, { id: 10, text: "a" }, { id: 10, text: "dup" }, { id: 20, text: "b" }, { id: 20, text: "lol"}])); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.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

where have you created the variable sortedFragments? (user story 5)

that was indeed the solution. What a tiny thing :sweat_smile: