Java Script Challenge

Hey ALL,

Trying to do this challenge but have no idea how. Anyone can help?

Write a function subLength() 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.

Examples:

subLength('Saturday', 'a'); // returns 6subLength('summer', 'm'); // returns 2subLength('digitize', 'i'); // returns 0subLength('cheesecake', 'k'); // returns 0

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else!

you could do it using a loop …
looping through characters in the string (using the index)
you’d probably need a variable to store the index of the first occurrence of that character
and another variable to find the next occurrence of that character

I hope that’s enough to help you get started.

1 Like

Thank you for the advice, I will do this now, hope will come thru …

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