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
-
3you are returning true when the first characters are equalFabricator– Fabricator2014年07月30日 23:58:13 +00:00Commented Jul 30, 2014 at 23:58
-
2Missing semicolons all over the placeArtOfCode– ArtOfCode2014年07月30日 23:59:46 +00:00Commented Jul 30, 2014 at 23:59
-
2Also, beware the "heyyy" string (slight NSFW language)Codeman– Codeman2014年07月31日 00:04:14 +00:00Commented 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?ThreeFx– ThreeFx2014年07月31日 00:06:27 +00:00Commented 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.taabma– taabma2014年07月31日 00:12:59 +00:00Commented Jul 31, 2014 at 0:12
3 Answers 3
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.
1 Comment
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
}
Comments
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.