Daily Coding Challenge - Word Compressor

Tell us what’s happening:

My code doesn’t pass

There is probably a bug in this challenge. It involves the code validation at the last test

My guess is, the expected output is wrong.

Your code so far

function compress(str) {
  const words = str.split(' ')
  const seen = []
  const output = []

  for(let word of words){
    if(seen.includes(word)){
      output.push(seen.indexOf(word)+1)
    }else{
      seen.push(word)
      output.push(word)
    }
  }

  return output.join(" ");
}

Your browser information:

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

Challenge Information:

Daily Coding Challenge - Word Compressor

https://www.freecodecamp.org/learn/daily-coding-challenge/2026-04-24

your output is wrong, you start having issues with at, when it appears the second time you say it’s the 12th word, but it’s the 16th

the number is the position of the word in the string, you need to count the words that have been already repeated

correct output:

lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16

your output:

lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 12

you are not counting the words that have already been replaced before at appear for the first time

Thank you for that correction