JavaScript Practice: Arrays, Loops, Objects, Iterators

* Remember to include a link to the exercise you need help with!


The problem states: 'Write a function sub length() that takes 2 parameters, a string and a single character. The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0'.

So I have here two solutions one using reduce and one not using, the issue is that i keep getting this error and not sure why exactly, it's a syntax error.

![image|390x384](upload://4YGbRKlhTg5JukbCxaPw4dUnUgU.png)

const subLength = (word, letter) => {

  return word.reduce((previousValue, currentValue) =>
  {

    if(previousValue === 'a' && currentValue === 'a' )
    {
      return word.indexOf(previousValue) - word.indexOf(currentValue);
    }

  });

  /*let characterIndex = 0;
  let secondcharacterIndex = 0;

  let count = 0;

 for(let i = 0; i < word.length; i++)
 {
   if(word[i] === letter)
   {
     
     count++
     
    }

 }

//console.log(count);

 if(count < 2 && count > 2)
 {
   console.log('here');
   return 0;
 }

 else
 {
   for(let i = 0; i < word.length; i++)
   {
     if(word[i] === letter)
     {
       //console.log('here');
       
       characterIndex = i;

     }

     //console.log(i);

     else if (word [i + 1] === letter && word[i + 1] != undefined  )
     {
        //console.log('here');
        //console.log(i);
       secondcharacterIndex = i;
            //console.log(i);

     }



   }
 }

 console.log('This first character is at: ' + ' ' + characterIndex);
 console.log('This two character is at: ' + ' ' + secondcharacterIndex);
 

 let distanceOfCharacters = Math.abs((characterIndex - secondcharacterIndex));

 return distanceOfCharacters;

}*/

console.log(subLength('computation', 'o'));

hi @mustafaal-obeid74498

thank you for posting this question. However, due to your code formatting, it poses quite a challenge for the community to assist you hence I’d advise you could read up here to find out how to better format your questions and codes.

Thanks! Looking forward to the amended.

can you please whats wrong with my formatting

I think im going for this solution, i don’t understand why it doesnt return the right output. for subLength('computation', 'o') and it did not return 9 . Logically the code makes sense, I dont know what you mean by formating.

// Write function below const subLength = (word, letter) => { let characterIndex = 0; let secondcharacterIndex = 0; let count = 0; for(let i = 0; i < word.length; i++) { if(word[i] === letter) { count++ } } //console.log(count); if(count < 2 && count > 2) { console.log('here'); return 0; } else { for(let i = 0; i <= word.length - 1; i++) { if(word[i] === letter) { console.log(i); characterIndex = i; } //console.log(i); else if (word [i + 1] === letter && word[i + 1] !== undefined ) { //console.log('here'); console.log(i); secondcharacterIndex = i; } } } console.log('This first character is at: ' + ' ' + characterIndex); console.log('This two character is at: ' + ' ' + secondcharacterIndex); return Math.abs((characterIndex - secondcharacterIndex)); } console.log(subLength('computation', 'o'));

Erm here? I’ve no idea what’s the error you’re trying to highlight.

Also, you could have understand how Markdown syntax is done correctly, and format your question using it so the effects can be achieved in the forum post.

Im not using the code on top anymore and its not an error it just not returning the right output for computation it should return 9 for computation but its returning 1

The purpose of the function is to find the distance between letters that are the same in a string

nevermind, i solved it

const subLength = (word, letter) => { let characterIndex = 0; let secondcharacterIndex = 0; let count = 0; for(let i = 0; i < word.length; i++) { if(word[i] === letter) { count++ } } //console.log(count); if(count < 2 || count > 2) { console.log('here'); return 0; } else { characterIndex = word.indexOf(letter); secondcharacterIndex = word.indexOf(letter, characterIndex + 1); } return Math.abs(characterIndex - secondcharacterIndex) + 1;
1 Like

Good to hear! I think your 2nd version is returning 1 because it is the way you want it programmed.

Unlike your 3rd version of code.

If you’re up for the challenge, try to optimized your codes further. As certain variable(s) might not be necessary.

how can i fix my second verison

The answer lies from Line 41 onward.

Observed how your variables are being overwritten. Then you’ll understand. :slightly_smiling_face:

im still confused, what do you by mean overwritten

Meaning, each time when your loop executes until word.length, what does
characterIndex and secondCharacterIndex stores?

Are you certain that they both store the numbers you need to get the number, 7? Or otherwise.

Ask yourself why does it not happened in that manner? Then from there, debug your syntax to understand at each loop, what does you code do.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.