Confirm the ending

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");
  1. Make sure you’re using the var keyword to declare variables.
  2. Rethink your if clause - what do you expect strSplit[strEndPosition] to be?

Maybe i’m wrong, but was thinking strSplit[strEndPosition] to be the last element of an array, or the last one of a string.

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?

I think the bolded statement is false.

When you are doing strSplit=str.split(" "); what do you get (type, value)?

When you are doing strEndPosition=(strSplit.length-1); what do you get (type, value)?

Use console.log() to confirm you’re getting what you think you’re getting.

Also you should look into String.substr() as suggested in the assignment’s description.

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) {  
  var strLen = target.length;

  if(str.substr(-strLen) === target){
    return true;
  } else {
    return false;
  }
}
confirmEnding('Bastian', 'n');
5 Likes

That’s good and easy approach! :slight_smile:

Instead of all of this:
if(str.substr(-strLen) === target){
return true;
} else {
return false;
}

You can use:
return str.substr(-strLen) === target;

3 Likes
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?

Hi Aldon2,

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.

Hope that helps :slight_smile:

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor

str=str.toLowerCase();
target=target.toLowerCase();
var ninja=str.split("");
var hello=target.split("");

ninja=ninja.reverse();
hello=hello.reverse();

for(var i=ninja.length;i>hello.length;i–)
{
ninja.pop();

}  

ninja=ninja.join(’’);
hello=hello.join(’’);

if(hello===ninja)
{return true;}
else{return false;}
return ninja;

}
confirmEnding(“He has to give me a new name”, “name”);

//try this code

1 Like

This is the very simple solution I came up with-

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");
2 Likes

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;
}

  else 
    break;
}

if(count<target.length)
return false;
else if(count===target.length)
return true;
else
return false;
}

confirmEnding(“Bastian”, “n”);

***confirmEnding(“Connor”, “n”) should return false. —only this statement runs wrong.help!!

i did it this way…

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;
}

confirmEnding(“Bastian”, “n”);

i use slice ()

function confirmEnding(str, target) {
  var num = str.length-target.length;
  var newString=str.slice(num);
 return newString == target;
   
}
1 Like

Hello

Could someone tell me why this doesn’t split the Bastian into an array with its letters separate?

function confirmEnding(str, target) {
var array = [];
array = str.split(’ ');

// “Never give up and good luck will find you.”
// – Falcor
return array;
}

confirmEnding(“Bastian”, “n”);

question, why did you make

var strLen = target.length;

instead of just making strLen = -1?

thanks

oh, and i see that there is no need to make an array in the first place.