Restore ~~a Coherent Narrative from an Array of Story Fragments~~ my sanity!

Tell us what’s happening:

I was doing quite well at this lab! I was just working out a few kinks in dedupeFragments() when all of a sudden every test was failing. As I say, this is code I was still working on, so console logs are commented out so they don’t clutter up my view of what the code is doing in real time, and normally before I submit it I go through and give my variables more conventional names (the single-letters just help me think better!) and god knows I haven’t even touched the last function yet, so this is far from the typical “why isn’t it working?”-type post, but I was relying on the code validator to tell me what my code was outputting as I went along (and also to know I was doing things right…you know, that sort of validation too!)

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(x){
  const r = [...x];
  const o = [];
  for (const a of r){
    if (a != undefined){
      o.push(a);
    };
  };
  if (o.length != r.length){
    console.log(`[COMPACTED] the array`)
  };
  return o;
};

const compactedShuffledFragments = compactFragments(shuffledFragments);

function sortFragments(r){
  return r.toSorted((a, b) => {return a.id - b-id});
};

const sortedFragments = sortFragments(compactedShuffledFragments);

function extractId(b){
  if (!Object.hasOwn(b, "id")){
    console.log("Error: id key not found");
    return;
  };
  const j = JSON.stringify(b);
  return j.slice(j.indexOf("\"id\":") + 5, j.indexOf(","));
};

function dedupeFragments(x){
  const [...r] = x;
  const [h, o, y] = [[], []];
  const mp = r.map(extractId);
  for (const jsd of mp){
    if (!h.includes(jsd)){
      h.push(jsd);
    };
  };
  for (let j = 1; j < r.length; j++){
    let i = j - 1;
    if (extractId(r[j]) == extractId(r[i])){
      r.splice(j, 1);
      //console.log("[DEDUPED] a thing")
    };
  };
  for (let j = 1; j < r.length; j++){
    let i = j - 1;
    if (extractId(r[j]) == extractId(r[i])){
      r.splice(j, 1);
      //console.log("[DEDUPED] a thing")
    };
  };
  return r;
};

const dedupedFragments = dedupeFragments(sortedFragments);

function fillMissingFragments(a){
  const b = [];
  for (let i = o; i <= 20; i++){
    let c = extractId(a[i+1]);
    if (i == c){
      b.push(a[i]);
    } else {
      b.push({ id: i, text: "[...]" });
    };
  };
  return b;
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:159.0) Gecko/20100101 Firefox/159.0 (I use Zen Browser not regular Firefox if that makes any difference!)

Challenge Information:

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

Hi @bamclowder,

Your sortFragments function is using toSorted, which is just another version of sort, which the instructions ask you not to use.

Array.prototype.toSorted() - JavaScript | MDN

Happy coding

Ah, I see, thanks! Just wondering, why don’t the instructions say anything about toSorted() if it’s also forbidden? I was instructed not to use sort(), so I looked at what I had available to me, and I decided the toSorted() method would transform the data in the requisite ways. I fail to see how this isn’t just basic problem-solving based on the instructions as they were given to me - I was asked to find an alternative means of sorting than using the sort() method, so I did just that.

the tests are checking if sort is ever called, it seems toSorted uses sort under the hood