I solved this challenge but I was wondering if there is a way to shorten the code without using any other string functions?

Tell us what’s happening:
Describe your issue in detail here.

   **Your code so far**

function confirmEnding(str, target) {
let b=str.length-1-target.length;
let str2="";
for(let i=str.length-1;i>b;i--){
   console.log(str[i]);
   str2+=str[i];
}
str2=reverseString(str2);
console.log(str2);
console.log(str2==target);
return str2==target;
}
function reverseString(str) {
 let a=str.length;
 let b=[];
 for(let i=0;i<str.length;i++)
 {
   b.unshift(str[i]);
 }
 str='';
 for(let i=0;i<a;i++){
 str+=b[i];
 }
 
 return str;
}

confirmEnding("Abstraction", "action");
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.154

Challenge: Confirm the Ending

Link to the challenge:

Using RegEx would be one way, but if you are fine with using string methods I was able to solve the challenge in one line using the slice method. Is there a reason you’d like not to use string methods?

1 Like

Because the freecodecamp in its current curriculum has not taught it so far. I would like to wait until later sections when they teach it to start using it.

Regex has been taught so asking, how would you do it using regex? Can you just give me an idea and not the code. (Paused here for a while and gave a hard thought lol)

Oh I think I remember now :grinning:, you just make a regex out of the target value and you search for it right. Holy shit, would’ve been so easy that is a few lines. Thanks for the suggestion! Happy to recall it!

1 Like

hi i made this solution of only utilizing a for loop, string indexing and length.

function confirmEnding(str, target) {
  let targetL=target.length
  let strL=str.length
  let end=''
  for (let i=strL-targetL; i<strL; i++){
    end+=str[i]
  }
  return end===target
}

PS: you can directly use the str.length values instead of assigning them to variables and shorten the code even more

Im afraid to use regex to solve the challenge, you will need to use the RegExp object and include the target value in the RegExp body, which is not a technique part of the curriculum.

1 Like

Here’s one way to solve it without relying on any string methods except length. You just compare character by character from the end.

Code
const confirmEnding = (str1, str2) => {
  const end1 = str1.length - 1
  const end2 = str2.length - 1

  for (let i = 0; i < end2; i++) {
    if (str1[end1-i] !== str2[end2-i]) return false
  }

  return true
}

In general, you want to take advantage of string methods provided by the language. You can have a one line solution if you use substring or slice.

Code with substring or slice
const confirmEnding2 = (str1, str2) => {
  return str1.substring(str1.length - str2.length) === str2
}

const confirmEnding3 = (str1, str2) => {
  return str1.slice(str1.length - str2.length) === str2
}
2 Likes

This is probably be the best solution for me. The first one. Thank you. Interesting to see different perspectives/ways of tackling the problem.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.