Confirm the ending

Confirm the ending
0

#1

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

#2
  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?

#3

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


#4

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?


#5

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.


#6

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.


#7
function confirmEnding(str, target) {  
  var strLen = target.length;

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

#8

That’s good and easy approach! :slight_smile:


#9

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

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


#11
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");

#12

@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?


#13

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:


#14

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


#15

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

#16

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!!


#17

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”);


#18

If you want to make your existing code a little shorter, you can have one return statement without using if comparison. See below:

function confirmEnding(str, target) {
  var compare = str.substring(str.length - target.length); 
  return compare == target;
}

It simply returns the evaluation of the the comparison (true or false).
The above can be converted to a one-liner by getting rid of the compare variable (see below).

function confirmEnding(str, target) { return str.substring(str.length - target.length) == target}

#19

i use slice ()

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

#20

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”);


#21

It does split it into an array. It is an array of 1 string.

[ 'Bastian' ]

If you want to split Bastian into an array of characters, then you would use:

array = str.split('');

Then the array would be:

[ 'B', 'a', 's', 't', 'i', 'a', 'n' ]

Your code had a space in the split function:

array = str.split(' ');