Intermediate Algorithm Scripting - Search and Replace

Tell us what’s happening:
Describe your issue in detail here.
I have completed this challenge, but I still do not understand why in this part:
else if(before[0] == lowerLetter )
{
return str.replace(before, after.toLowerCase())
}
when i use strict equality operator, this test does not pass:
myReplace(“I think we should look up there”, “up”, “Down”)
but when I use equality operator, it passess
I already known the difference between these two, but I still do not understand why.Can you explain that to me, thank you

   **Your code so far**
function myReplace(str, before, after) {
 let upperCaseLetterInBefore = before.match(/[A-Z]/)
 let lowerLetter = before.match(/[a-z]/)

if(before.indexOf(upperCaseLetterInBefore) === 0) {
 return str.replace(before,after[0].toUpperCase() + after.slice(1) )
}

else if(before[0] == lowerLetter ) 
{
return str.replace(before, after.toLowerCase())
} 
else 
{
return str.replace(before, after) }
}

console.log(myReplace("I think we should look up there", "up", "Down"));
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Search and Replace

Link to the challenge:

Add these:

console.log(before[0]);
console.log(lowerLetter);

above your if statement. You actually got lucky in solving it this way as the return value of before.match(/[a-z]/) is an array. The first index has the matching letter, so use the == operator the array get coerced into a string of the first element. Using the === would validate the two values have the same type. They do not, so the expression evaluates to false.

1 Like

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