Basic Algorithm Scripting - Confirm the Ending

Is this way of solving this challenge is okay ???

Your code so far

function confirmEnding(str, target) {
let end=str.substring(str.length-(target.length));
console.log(end);

if(end===target)
{
  return true;
}
else
{
 return false;
}
}
confirmEnding("Congratulation","on");

Your browser information:

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

Challenge: Basic Algorithm Scripting - Confirm the Ending

Link to the challenge:

Works great - nice job!

In fact, we can cut it down a tiny bit without losing its readability and without changing the prime part of your solution.

These are not necessary to make them “better” at this point, as your code works fine — just food for thought:

function confirmEnding(str, target) {
  let end = str.substring(str.length - target.length);

  return end === target;
}

or even

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

Both of those return values evaluate to a boolean true or false before returning. Since your function is expecting a boolean value in its return, it works out nicely.

Keep it up!

1 Like

minor - I recommend standard formatting so its easier for people to read your code:

function confirmEnding(str, target) {
  const end = str.substring(str.length - (target.length));
  // console.log(end);
  if (end === target) {
    return true;
  } else {
   return false;
  }
}
confirmEnding("Congratulation","on");
2 Likes

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