Can someone please explain the solution to the second practice question? I am lost.
An example solution to this problem could be to iterate through the word, str, looking at each letter to see if it matches the provided character, char. If str contains less than 2 or more than 2 instances of char, return 0. Otherwise, there are 2 instances of char in str, and we want to return the number of letters between those 2 instances of char, inclusive (counting both instances in our count of the number of letters). We store the index locations of both instances of char, and return the difference between the two index locations, plus 1 (to make it inclusive of both instances of char).
Here is an example:
What is the expected return? That is what to focus upon in your logic. The question lays out the parameters:
return the length between them including the 2 characters
We’re also given to exclude any given sample that has less than two and more than two from our selection group. The logic around this is rudimentary and intuitive. Your brain has figured this out, just give it a night’s sleep to tell you.
Step 1 : (for your brain to absorb)
What is the boundary indicator? We should examine the presented data and parse out all the instances.
Step 2: Have we the requisite number of instances of the boundary value?
If so, congregate the string value between the boundaries and cap them off with their respective character to complete the string .
Step 3:
Simple, return the string length.
Along the way we do need to be able to flag situations that won’t work. That is part of the objective of this lesson, if I’m reading things right.
Splitting a sequence on a margin (separator) will give us enumerable segments, as in, 0, 1, 2, dot dot dot as their indices. We want this sample to have exactly three of these indices. That being the case, problem solved and we move on. The real logic is in the discarding of the cases we cannot use.
Most of this i understand. But, why is this code present:
else if (str[i] === char) {
return 0;
Also, why is
else if ((i === str.length-1)
Present in the code block:
else if ((i === str.length-1) && ((count < 2) || (count > 2))) {
return 0;
}
Most of this i understand. But, why is this code present:
else if (str[i] === char) {
return 0;
This covers the case where the letter at the index location you’re looking at is the char value, occurring for the third time in the given word (like in the case where str = ‘momentum’ and char = ‘m’). In that case, we return 0.
Also, why is
else if ((i === str.length-1)
Present in the code block:
else if ((i === str.length-1) && ((count < 2) || (count > 2))) {
return 0;
}
The code else if ((i === str.length-1)
is checking if it is true that we are at the end of the iteration, looking at the last letter in str.
This code block covers the case where we have iterated through each letter in str, and your count is less than or greater than 2 (like in the case where str = ‘cheesecake’, and char = ‘k’). Because the char value doesn’t appear exactly twice in the provided str, we return 0.
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.