it’s Not working for those 2 cases…need HeeeeLP!!!
confirmEnding(“Bastian”, “n”) should return true.
confirmEnding(“He has to give me a new name”, “me”) should return true.
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
strSplit=str.split(" ");
strEndPosition=(strSplit.length-1);
if(strSplit[strEndPosition]===target){
return true;}
else {
return false;}
}
confirmEnding("Bastian", "n");
Yes, you’re absolutely right. I did not think about what I was trying to say as carefully as I should have. Sorry! Here’s what I meant: When you split the string, “Bastian”, up with str.split(" "), what do you expect strSplit[strEndPosition] to be?
He’s rigth, your code will work just if target it’s a whole word, because you’re splitting str into whole words by using " ". You should take a look at this: http://www.w3schools.com/js/js_string_methods.asp. Maybe using slice, substring o substr methods (the one you prefer) may be a better aproach.
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var strEnd = str.substr(str.length - target.length);
return strEnd === target;
}
confirmEnding("Bastian", "n");
@Beekey@Ediur your code works great! I battled with this problem for a while! I would like to ask if it is possible for you to explain the code to me, please? My confusion stems from the fact that str.substr returns an int as does target.length yet they pass the test cases! what I’m I missing here?
str.substr returns a string, target is also a string. We compare them with the logical operator === and get either true or false as a result which is then returned.
target.lenght is an int and it’s used within the substr prototype to isolate the correct substring of the given str.
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var subStr = str.substring(str.length-target.length);
return subStr===target;
}
confirmEnding("Bastian", "n");
function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var count=0;
for(var i=0;i<target.length;i++)
{
if(str.substr(str.length-i)===target.substr(target.length-i))
{
++ count;
}
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
var compare = str.substring(str.length - target.length);
if (compare == target){
return true;
} else {
return false;
}
// – Falcor
//return str;
}