Hello guys, I’m not getting the expected result from my code. I read it 1000 times and still not finding the mistake.
the function below needs to check if a string (first argument, str
) ends with the given target string (second argument, target
).
function confirmEnding(str, target) {
let start= str.length-target.length;
let check= "";
for (let i=start ;i<target.length ; i++) {
check+= str[i];
}
return target === check;
}
confirmEnding("Bastian", "n"); // true
Thanks a lot
ilenia
2
start
is 6 but target.length
is 1, your loop never happens.
In this case the return statement is
return "n" === ""; // this is false
Seems you just need to compare a substring (at the end) of str
to target
and return whether they match or not. Do you even need a loop?
Read this: https://www.w3schools.com/js/js_string_methods.asp
Specifically the ‘Extracting String Parts’ section. Sure one of those might help you out.
A small hint:
const aSubstring = 'Congratulation'.slice(-2);
console.log(aSubstring); // on