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:

I cannot pass task 10 which instructed that compactedShuffledFragments should be an array with no undefined elements

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(shuffArr){
  let compactedShuffledFragments = [];
for(let i =0; i<shuffArr.length; i++){
  if(shuffArr[i] === undefined){
    console.log("[COMPACTED]")
  }
  else{
    compactedShuffledFragments.push(shuffArr[i])
  }
}
return compactedShuffledFragments;
}
const finalFragments = compactFragments([{ id: 1, text: "Hello" }, undefined, { id: 2, text: "World" }]);
console.log(finalFragments);



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

where are you doing this?

You should declare a variable named compactedShuffledFragments and assign it the result of calling compactFragments with the shuffledFragments array.

Tell us what’s happening:

I can’t figure out what is wrong with the third function block. It is showing a TypeError: cannot read properties of undefined(reading ‘id’).

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(shuffArr){
  let newShuff = shuffArr.slice();
  let shuffFragments = [];
for(let i =0; i<newShuff.length; i++){
  if(newShuff[i] === undefined){
    console.log("[COMPACTED]")
  }
  else{
    shuffFragments.push(newShuff[i])
  }
}
return shuffFragments;
}
const compactedShuffledFragments = compactFragments(shuffledFragments);
function sortFragments(shuffArr){
  const arrToBeSorted = shuffArr.slice();
for(let i =0; i<arrToBeSorted.length-1; i++){
  for(let j=0; j<arrToBeSorted.length - 1-i; j++){
  if(arrToBeSorted[j].id > arrToBeSorted[j+1].id){
    const temp = arrToBeSorted[j];
arrToBeSorted[j] = arrToBeSorted[j+1];
arrToBeSorted[j+1] = temp;
  
}
  }
}
return arrToBeSorted;
}
const sortedFragments = sortFragments(compactedShuffledFragments);
function dedupeFragments(dedupeFrag){
const sortedAbtToDedupe = dedupeFrag.slice();
const resultOfDedupe = [];
for(let i=0; i<sortedAbtToDedupe.length;i++){
  if(sortedAbtToDedupe[i].id === sortedAbtToDedupe[i+1].id){
    console.log("[DEDUPED]")
  }
  else{
resultOfDedupe.push(sortedAbtToDedupe[i])
  }
}
return resultOfDedupe;
}
const dedupedFragments = dedupeFragments([{ id: 1, text: "first" }, { id: 1, text: "dup" }, { id: 2, text: "second" }]);
console.log(dedupedFragments);

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

Hi @Emmysifire

What happens when i + 1 is greater than the length of the array?

Happy coding

Hi @Emmysifire

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Happy coding

I am stuck here, I do not know how to complete the fillMissingFragments function.

any Idea?

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(shuffArr){

  const newShuff = shuffArr.slice()

  let shuffFragments = [];

for(let i =0; i<newShuff.length; i++){

  if(newShuff[i] === undefined){

    console.log("[COMPACTED]")

  }

  else{

    shuffFragments.push(newShuff[i])

  }

}

return shuffFragments;

}

const compactedShuffledFragments = compactFragments(shuffledFragments);

function sortFragments(shuffArr){

  const arrToBeSorted = shuffArr.slice();

for(let i =0; i<arrToBeSorted.length-1; i++){

  for(let j=0; j<arrToBeSorted.length - 1-i; j++){

  if(arrToBeSorted[j].id > arrToBeSorted[j+1].id){

    const temp = arrToBeSorted[j];

arrToBeSorted[j] = arrToBeSorted[j+1];

arrToBeSorted[j+1] = temp;

  

}

  }

}

return arrToBeSorted;

}

const sortedFragments = sortFragments(compactedShuffledFragments);




function dedupeFragments(dedupeFrag){

const sortedAbtToDedupe = dedupeFrag.slice();

const resultOfDedupe = [];

for(let i=0; i<sortedAbtToDedupe.length;i++){

  if(i>0 && sortedAbtToDedupe[i].id === sortedAbtToDedupe[i-1].id){

    console.log("[DEDUPED]");

  }

  else{

resultOfDedupe.push(sortedAbtToDedupe[i])

  }

}

return resultOfDedupe;

}

const dedupedFragments = dedupeFragments(sortedFragments);

function fillMissingFragments(dedupedArr){

const dedupedAbtToFill = dedupedArr.slice();

for(let i=0; i<dedupedAbtToFill.length; i++){

  let numberOfFirstId = dedupedAbtToFill[0].id;

  let numberOfLastId = dedupedAbtToFill[dedupedAbtToFill.length -1];

  for(let j=numberOfFirstId; j<numberOfLastId; j++){

    

  }

}




}

const filledFragments = fillMissingFragments(dedupedFragments);

console.log(filledFragments);

Hi @Emmysifire,

You should create a function named fillMissingFragments that takes a sorted array of fragments and returns a new array with missing fragments filled with placeholder objects. You should define missing fragments as gaps in the sequence between the lowest and highest id. The placeholder objects should have the format { id: missingId, text: "[...]" }. For each placeholder added, the function should log a message to the console. The message should start with the prefix [FILLED].

Can you say in your own word what this user story is asking you to do?

Happy coding

The assembledStory function block is not passing the test especially task 30.

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(shuffArr){

  const newShuff = shuffArr.slice()

  let shuffFragments = [];

for(let i =0; i<newShuff.length; i++){

  if(newShuff[i] === undefined){

    console.log("[COMPACTED]")

  }

  else{

    shuffFragments.push(newShuff[i])

  }

}

return shuffFragments;

}

const compactedShuffledFragments = compactFragments(shuffledFragments);

function sortFragments(shuffArr){

  const arrToBeSorted = shuffArr.slice();

for(let i =0; i<arrToBeSorted.length-1; i++){

  for(let j=0; j<arrToBeSorted.length - 1-i; j++){

  if(arrToBeSorted[j].id > arrToBeSorted[j+1].id){

    const temp = arrToBeSorted[j];

arrToBeSorted[j] = arrToBeSorted[j+1];

arrToBeSorted[j+1] = temp;

  

}

  }

}

return arrToBeSorted;

}

const sortedFragments = sortFragments(compactedShuffledFragments);




function dedupeFragments(dedupeFrag){

const sortedAbtToDedupe = dedupeFrag.slice();

const resultOfDedupe = [];

for(let i=0; i<sortedAbtToDedupe.length;i++){

  if(i>0 && sortedAbtToDedupe[i].id === sortedAbtToDedupe[i-1].id){

    console.log("[DEDUPED]");

  }

  else{

resultOfDedupe.push(sortedAbtToDedupe[i])

  }

}

return resultOfDedupe;

}

const dedupedFragments = dedupeFragments(sortedFragments);

function fillMissingFragments(dedupedArr){

const dedupedAbtToFill = dedupedArr.slice();

if(dedupedAbtToFill.length ===0){

  return []

}

const resultOfFilled = []

let expectedId = dedupedAbtToFill[0].id;

for(let i=0; i<dedupedAbtToFill.length; i++){

  let currentItem = dedupedAbtToFill[i];

  while(expectedId < currentItem.id){

    console.log(`[FILLED]`);

    resultOfFilled.push({id: expectedId, text: "[...]"});

    expectedId++;

  }




    resultOfFilled.push(currentItem);

    expectedId++;

  }

  return resultOfFilled;

}





const filledFragments = fillMissingFragments(dedupedFragments);

function assembleStory (fillFragments){

  let assembledArr = fillFragments.slice()

  let result = ""

  for(let i=0; i<assembledArr.length; i++){

    result += assembledArr[i].text +"\n"

    

  }

return result;

}

console.log(assembleStory(filledFragments));

Is that what you should be passing in as a function parameter?

Also, there should be no space between the function name and the opening parenthesis in your function definition.

Happy coding

That’s my chosen name for the name of the parameter. Does it matter?

Oops! No; it doesn’t. Your function call is sending in the correct array.

Try testing the failed Test #30 like this and compare to what that test says your string should return:
console.log(JSON.stringify(assembleStory([{ id: 1, text: "Hello" }, { id: 2, text: "World" }])));

What are the backward strokes for? I cannot pass task 30 and 32 .

I’ve edited my post to remove those. They were not intended.