Function subLength()

Thanks @ghost_in_the_shell I was losing my mind. This feels like some of the first code I ever wrote haha. Crazy how far I’ve come since here.

Happy to hear that you kept coding and improving! :slight_smile:

I recently started learning how to code myself and had no idea if it was worth even replying to this post since it was created some time ago.
I hope it will come in handy to someone else on the way.

1 Like

I finally had someone like an answer I gave on a post I made after I discovered the answer. It’s always nice to get feedback. The thing that was throwing me off was the code was working correctly even with that statement in it. Just codecademy didn’t like it.

Yeah, I sometimes have the same issue. It hasn’t been frustrating, yet. However, the loudly project that I recently went through was a drag. For some reason all of the node.js commands were not working for me.

Hello

Could someone help me ? Why my code output comes out '0 ’ in all ? The argument said ’ If there are less than 2 or more than 2 occurrences of the character the function should return 0." which i did but it is showing 0 , what is the reason please ?
thanks

const subLength = (str, char) => {
let x = str.split(' ').filter(y => y == char)
if (x.length < 2 ) {
  return 0;
} else if ( x.length > 2) {
 return 0;
}
else if (x.length == 2 ) {
  return str.lastIndexof(chr) - str.indexOf(char) +1 } 
  else {
    return undefied; 
  }
}

console.log (subLength('Saturday' , 'a'));
console.log (subLength('summer', 'm')); 
console.log (subLength('digitize', 'i')); 
console.log (subLength('cheesecake', 'k'));

Try printing to see what is happening. For example:

I edited your post to format the code for readability. Please review: How do I format code in my posts?

Hello
Thank you, i have read ’ How do i format code in my posts? ’ . I understood my codes should be put in ``` there right? I also tried console.log(x) under x=str.split (’ ’ ) , it is showing : , 0 , , 0 , , 0 . I cannot find the bug yet :frowning:

const subLength = (str, char) => {
let x = str.split(' ').filter(y => y == char)
console.log(x);
if (x.length < 2 ) {
  return 0;
} else if ( x.length > 2) {
 return 0;
}
else if (x.length == 2 ) {
  return str.lastIndexof(chr) - str.indexOf(char) +1 } 
  else {
    return undefied; 
  }
}

console.log (subLength('Saturday' , 'a'));
console.log (subLength('summer', 'm')); 
console.log (subLength('digitize', 'i')); 
console.log (subLength('cheesecake', 'k'));

The first problem you’re having is this portion:

let x = str.split(' ')

Since you’re splitting with a space, which none of the words have, it’s returning an array of just one element with the whole word. Example: 'Saturday' becomes [ 'Saturday' ]. Then you’re trying to use filter() on it and comparing it to only one character, so it will never have any matches.

If you used split('') without a space, then you’d have something to work with and can continue debugging and exploring your idea for the solution.

1 Like

Thank you @selectall , so now i changed .split without space and another error came out . It said as below :
return str.lastIndexof(char) - str.indexOf(char) +1 ;
^TypeError: str.lastIndexof is not a function

Why this is showing please?

``const subLength = (str, char) => {
let x = str.split(’’).filter(y => y == char)
if (x.length > 2 ) {
return 0;
}
else if ( x.length < 2) {
return 0;
} else if (x.length === 2 ) {
return str.lastIndexof(char) - str.indexOf(char) +1 ;
}
}

console.log (subLength(‘Saturday’ , ‘a’));
console.log (subLength(‘summer’, ‘m’));
console.log (subLength(‘digitize’, ‘i’));
console.log (subLength(‘cheesecake’, ‘k’));

`

@arc2779423039 The error message is giving you an important clue. If it’s saying that it isn’t a function and you know it should be, then double check the spelling. Also, keep in mind that JavaScript is case-sensitive so lastIndexof is not the same as lastIndexOf

1 Like

@selectall
Thank you, i have no more error but the problem is all outputs are coming out 0,0,0,0 instead of 6,2,0,0.
Could you see where i am wrong ?

``
const subLength = (str, char) => {
let x = str.split(’ ').filter(y => y == char)
if (x.length > 2 ) {
return 0;
}
if ( x.length < 2) {
return 0;
}

if (x.length === 2 ) {
return str.lastIndexOf(char) - str.indexOf(char) +1 }
}

console.log (subLength(‘Saturday’ , ‘a’));
console.log (subLength(‘summer’, ‘m’));
console.log (subLength(‘digitize’, ‘i’));
console.log (subLength(‘cheesecake’, ‘k’));

Yes, you’re back to the original issue you posted about because you’re splitting by space. You corrected it in a post after that, but you may have gone back to an earlier version of the code by mistake.

1 Like

thanks I got it now .

could someone help me shed some light on why the following code didn’t work? thank you

const subLength = (strg, char) => {
  let count;
  let boolean;

    for (let i = 0; i < strg.length; i++){
      if(strg.charAt(i) === char && count < 2) {
        boolean = true;
      } else {
        boolean = false;
      } 
      while (boolean){
        count++;
      }
    }

When boolean is true, this will be an infinite loop.

so the condition that breaks out of the while loop has to be defined within the while loop?

In this case, yes. Something in the change of count should trigger the break.

1 Like

My code looks like it should work (at least to me), but it doesn’t work (it only returns zero). I’m just curious if the logic I was going for is in the right direction or if I was way off. Thanks!

const subLength = (str, char) => {
let indices = ;
for(let i = 0; i > str.length; i++){
if(str[i] === char){
indices.push(i);
}
};

if(indices.length === 2){
return (indices[1] - indices[0]) + 1;
} else {
return 0;
}
};

Are we being given a character to use as endpoints in a new segment?

4 posts were split to a new topic: Why would this code not be accepted?