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.

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'));
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.
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