1

Here is the correct code for evaluating if two strings are equal in length and character.

function equal(x, y) {
 if(x.length != y.length)
 return false
 var i = 0;
 while(i < x.length) {
 if(x.charAt(i) != y.charAt(i))
 return false
 i++
 }
 return true
 }
equal("hello", "hello") // true
equal("hello", "heyyy") // false

I modified it on my own in which I think logical for me. But my code(shown below) returns true while their character content is different. Can any one help me what is wrong with my version?

function equal(x, y) {
 if(x.length != y.length)
 return false
 var i = 0;
 while(i < x.length) {
 if(x.charAt(i) != y.charAt(i))
 return false
 else return true
 i++
 }
}
equal("hello", "hi") //false
equal("hello", "heyyy") //true
Spencer Wieczorek
21.6k7 gold badges47 silver badges57 bronze badges
asked Jul 30, 2014 at 23:54
6
  • 3
    you are returning true when the first characters are equal Commented Jul 30, 2014 at 23:58
  • 2
    Missing semicolons all over the place Commented Jul 30, 2014 at 23:59
  • 2
    Also, beware the "heyyy" string (slight NSFW language) Commented Jul 31, 2014 at 0:04
  • Who upvotes this? There is even working code presented as comparison, so what is the problem with trying to debug the code and actually actively do something? Commented Jul 31, 2014 at 0:06
  • I am not a pro like most of you and I need to understand the basics before debugging my fault. Commented Jul 31, 2014 at 0:12

3 Answers 3

5

It's because you return true on the first iteration of the loop:

 if(x.charAt(i) != y.charAt(i))
 return false
 else return true

You need to return true at the end of the function:

function equal(x, y){
 if(x.length != y.length)
 return false
 var i = 0;
 while(i < x.length){
 if(x.charAt(i) != y.charAt(i))
 return false
 i++
 }
 return true
}

Also personally, I'd do this in a for loop :)

You also might run into a problem with things being out of bounds. Here's a demo showing what happens when you have inconsistent lengths. Thankfully you do a length check, though, so this shouldn't be a problem.

answered Jul 30, 2014 at 23:58
Sign up to request clarification or add additional context in comments.

1 Comment

+1. This is what I wanted to have written but you beat me to it.
1

The problem is your use of the return statement. The first example evaluates to false because the two strings are not both of the same length. The second is a false positive because you return true and exit the loop if any character is equal to another and not if all characters are equal.

To fix it just move the return statement:

function equal(x, y){
 if(x.length != y.length)
 return false
 var i = 0;
 while(i < x.length){
 if(x.charAt(i) != y.charAt(i))
 return false
 // else return true | move this to bottom to ensure correctness
 i++
 }
 return true // here if nothing was found in the first two steps, they must be equal
}
answered Jul 30, 2014 at 23:59

Comments

1

You return true in the first iteration of the loop if the characters are equal. This basically guarantees a loop exit after just one character, which is not what you want. Delete the else return true clause and add a return true after the while loop and you should be good to go.

answered Jul 30, 2014 at 23:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.