Owais93
September 9, 2019, 6:12pm
1
I need help understanding why my code doesn’t work.
code is given below.
function confirmEnding(str, target) {
let newString = ' ';
if (target.endsWith('n')) {
newString = target;
return true;
}
return false;
}
confirmEnding("Bastian", "n");
The link of the challenge is also given below.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending
OJL
September 9, 2019, 6:23pm
2
This is true
function confirmEnding(str, target) {
let newString = ' ';
if (str.endsWith(target)) {
newString = target;
return true;
}
return false;
}
but you get this error
Do not use the built-in method .endsWith() to solve the challenge.
So you need an other point of view.
Owais93
September 9, 2019, 6:26pm
3
@OJL brother, is my given code correct, if built in method was allowed to use in the challenge?
OJL
September 9, 2019, 6:30pm
4
You are not far :
this work :
function confirmEnding(str, target) {
if (str.endsWith(target)) {
return true;
} else {
return false;
}
}
Owais93
September 9, 2019, 6:32pm
5
Ahh I see…
lets try with another approach.
OJL
September 9, 2019, 6:54pm
6
Hi
try this beautiful approach using regex :
function confirmEnding(str, target) {
let patt = new RegExp(target+ "$");
return patt.test(str);
}
Owais93
September 9, 2019, 7:08pm
7
@OJL please check the code below. why isn’t that running. Do I need to make a regex first?
function confirmEnding(str, target) {
let check = /str$/;
if (check == target){
return true;
}
return false;
}
confirmEnding("Bastian", "n");
OJL
September 9, 2019, 7:11pm
8
function confirmEnding(str, target) {
let check = new RegExp(target + "$");
return check.test(str);
}
Owais93
September 10, 2019, 3:18pm
9
@OJL I’ve checked the previous Regex coding challenges in which we were taught this method given below.
function confirmEnding(str, target) {
let regex = /str$/;
let result = regex.test(str);
return result;
}
confirmEnding("Bastian", "n");
In your code below please define what is new keyword and RegExp(target+ “$”);
function confirmEnding(str, target) {
let patt = new RegExp(target+ "$");
return patt.test(str);
}
OJL
September 10, 2019, 3:51pm
10
Hello
I had take a look at your activity in FCC forum , I don’t expect this kind of questions from one in your level.
Owais93
September 10, 2019, 4:02pm
11
@OJL I am a newbie programmer not a pro, learning to code. All previous challenges are mixed in my head.
OJL
September 10, 2019, 4:13pm
12
Hi
I’m also beginner, but try to finish the current chapter and understand the concept behind before moving to the next , especially when we are learning the basis.