Reputation: 543
I've been working on learning JS, and I can't seem to figure out why my boolean values keep coming back either always true or always false.
So I believe I understand the basics of the truthy/falsy situation in JS, but I can't seem to get it right. I know that there are data type issues, (can't make different data types do different things).
function lastCharacter(char1, char2) {
if (char1[-1] === '' && char2[-1] === '') {
return true;
} else {
return false;
}
}
console.log(lastCharacter('apple', 'pumpkine'));
Or
function lastCharacter(char1, char2) {
if (char1[-1] === char2[-1]) {
return true;
} else {
return false;
}
}
console.log(lastCharacter('apple', 'pumpkina'));
Define a function lastCharacter
that accepts two strings as arguments.
lastCharacter
should return true if both strings end with the same character.
Otherwise, lastCharacter
should return false.
They either return always true or always false. Can anyone help me?
Upvotes: 1
Views: 320
Reputation: 386604
You need a different method for getting the last character of a string, preferably with String#slice
and a negative value for getting the last one. Then compare and return the result.
function lastCharacter(string1, string2) {
return string1.slice(-1) === string2.slice(-1);
}
console.log(lastCharacter('apple', 'pumpkine'));
A comparison between taking the last character of an empty string by using an index -1
which returns undefined
and slice
, which returns an empty string.
var x = '';
console.log('#' + x.slice(-1) + '#');
console.log('#' + x[x.length -1] + '#');
Upvotes: 3
Reputation: 138267
There are no negative array indexes in JavaScript, instead of char1[-1]
you have to use char1[char1.length - 1]
.
Accessing one of a strings characters (e.g. "abc[1]
) will always have a length of 1, it will never be equal to ""
. Your second function makes more sense.
Also
if(condition) { return true; } else { return false; }
is equal to
return condition;
Upvotes: 0
Reputation: 413727
The notation [-1]
does not implicitly mean "one character from the end of the string" in JavaScript. You can use str[str.length - 1]
. (If you expect possible empty source strings, you'd want to check for that too to avoid ending up with exactly the same problem.)
Instead of an if/else
that returns either true
or false
, just return the results of the logical expression:
return char1[char1.length - 1] === '' && char2[char2.length - 1] === '';
Both the ===
comparisons return either true
or false
anyway, so the overall expression value has to be one of those. In general however if you want to make absolutely sure that you end up with a boolean, you can prefix an expression with !!
to force the standard JavaScript "truthy-falsy" evaluation:
return !!(expression);
Upvotes: 0
Reputation: 37755
You can use slice
function lastCharacter(char1, char2) {
return char1.slice(-1) === char2.slice(-1)
}
console.log(lastCharacter('apple', 'pumpkina'));
console.log(lastCharacter('apple', 'snake'));
Or you can just access the last index
function lastCharacter(char1, char2) {
return char1[char1.length-1] === char2[char2.length-1]
}
console.log(lastCharacter('apple', 'pumpkina'));
console.log(lastCharacter('apple', 'snake'));
Upvotes: 2