Confirm the ending-could you provide sources?

Hi everyone,
I want to solve this challenge on my own without looking at solutions, please could you provide relevant links that could help me to work this challenge out by myself?

It says This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead----But there are no links to substrings methods, and the hint only leads to the solution

I feel like I am always looking at solutions and not able to write my own

Your code so far


function confirmEnding(str, target) {
return str;
}

confirmEnding("Bastian", "n");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Confirm the Ending

Link to the challenge:

you could do it with loops

or you have learned some substring methods like slice

you could also try googling for a substring methods, you can search for other sources of info

1 Like

Hi @Karem1986!

w3schools is great to always have open in one tab as you can look up any function that is mentioned in passing on any of your online courses

So for this one you’d want to look at:
https://www.w3schools.com/jsref/jsref_substring.asp

There’s also substr() which has a very similar functionality.

Good luck! :slight_smile:

1 Like

function confirmEnding(str, target) {

for(let i = 0; i < str.legth; i++) {

str[i].slice(target)

}

  return str;

}

confirmEnding("Bastian", "n");

This works out better than the last code but is still not ticking all conditions:

function confirmEnding(str, target) {

  let myCheck = true;

for(let i = 0; i < str.legth; i++) {

myCheck = str[i].slice(target)

}

  return myCheck;

}

confirmEnding("Bastian", "n");

Thanks to both!
i am using slice and works for the true cases
How could I include the false conditions too?

function confirmEnding(str, target) {

  let myCheck = true;

for(let i = 0; i < str.legth; i++) {

myCheck = str[i].slice(target)

}

  return myCheck;

}

confirmEnding("Bastian", "n");

At least I havent look at the solutions and i am trying :slight_smile:

you have various issues

first, str.legth is wrong, it is spelled str.length.
The only reason for which it returns true correctly it is because the for loop doesn’t execute for this spelling error.

Then, inside the loop you have myCheck = str[i].slice(target) which doesn’t have much sense. First, because str[i] is one single character, and second because slice takes one or more arguments but these arguments should be numbers. I suggest you return where you learned about slice or read the documentation about it.
And then, you assign it to myCheck, which was originally a boolean, and now it isn’t anymore.

I suggest you try again with something a bit more planned out. If you had to do this with pen and paper, how would you do it?

Thank you.
I corrected the spelling mistake with length. Now it does not work for any of the conditions.
slice() takes two parameters -
wich are numbers so which numbers should I use here?, since I dont get where is the end of the str
with pen and peper i would use:
myCheck = str[i].slice(0,9)

yep, before you had just a return true every time so you had the right answer half the time

that’s part of what you should come up with
but you have at your disposal str.length and target.length, and some math

what’s your reasoning for this line? why would you do this with pen and paper? what’s slice if you are using pen and paper?

on pen and paper slice()
will be to copy or extract the contents of each string to a new string but with a true or false depending on the end of the string.

I am lost, I dont know what to do , what math should I follow, is there any math() for this challenge?
Or I should look at the solution and understand what is doing?
The challenge should provide steps to follow or hints that lead me in the right path
But I am aware I need to come up with this myself, it is very difficult for me as I am a begginer
Thanks for you help!!

I found this:

You may need to check again what slice does: it just copy the string, there is no true/false involved

for example

let str = "abcdefghijklmn";
let newstr = str.slice(3); // newstr is "defghijklmn"

I really suggest you look up again what slice does:

just basic arithmetic, really

and that’s the solution, do you want to read it or solve this yourself?

ok, you need to confirm that they end at the same way
so, there is a part of the string str that you need to compare with the string target

if you take pen and paper we could do it for example:

Step 1 (click to show/hide)
  • measure the length of target (for Bastian and n is 1)
Step 2 (click to show/hide)
  • measure the length of str (it is 7)
Step 3 (click to show/hide)
  • calculate at what index I need to start checking to confirm the endind:
  • str has length 7, target has length 1, and 7-1 is 6, so I can ignore the first 6 letters, and start checking at index 6 (the 7th letter).
Step 4 (click to show/hide)
  • I can get the part of str to compare to target, from Index 6 and following which is just 'n'
Step 5 (click to show/hide)
  • The ending of str is equal to target and I can return true

now that the algorithm has been written in plain text, it can be converted to code

1 Like

Thank you
I will look again at what slice does (copy a string)
I will work out which arithmetic I need to use
Then I will update you:)

Why the length of str is 7?
I am confused

How many letters there are in “Bastian”?
There are general steps, applied to the str and target in your code

1 Like

Got it!
Now I have worked out the comparison and applied the comparison with -
function confirmEnding(str, target) {

let myCheck;

for(let i = 0; i < str.length; i++) {

if(str.length[i]-target.length) //Comparison between str and target

myCheck = str[i].slice(6)

}

return myCheck;

}

confirmEnding(“Bastian”, “n”);

But I still need to work out the other steps as this does not work yet

you have still various issues, I suggest you delete what you have, and try again.

what are you doing with str.length[i]-target.length? str.length is a number, it doesn’t have anything that you can access with indexes
Like, str[i] is a string of length one, it doesn’t have sense to use slice on it

so, I suggest, try to delete everything and do things step by step

you can use this tool to help visualize what’s happening:
http://pythontutor.com/javascript.html

I did, and I decided to look at the article that provides the sol with substr()
I did not know that you have to use within the if statement -target.length to count the number from the end of the string.

function confirmEnding(str, target) {

if(str.substr(-target.length) === target) {

return true;

} else {

return false;

}

}

confirmEnding(“Bastian”, “n”);

I think it was better I looked it up. Now I can define steps by steps in the next challenge.

Hope you understand and thanks for your help :slight_smile: