Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Javascript function

TASK: Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

MY SOLUTION:

function solution(str, ending) {
  if (str.slice(-1) === ending.slice(-1)) {
    return true;
  }
  return false;
}
console.log(
  solution('sumo', 'omo') // returned false!!!
)  

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use String.prototype.endsWith.

function solution(str1, str2) {
  return str1.endsWith(str2);
}

console.log(solution("football", "ball")); // true
console.log(solution("football", "foot")); // false
console.log(solution("football", "oot")); // false

If you want the comparison to be case insensitive, then you can compare the lowercase version of both the strings.

function solution(str1, str2) {
  return str1.toLowerCase().endsWith(str2.toLowerCase());
}

console.log(solution("FOOTBALL", "ball")); // true
console.log(solution("footBall", "BALL")); // true

about 4 years ago · Juan Pablo Isaza Report

0

You can do something like this

function solution(str, ending) {
  if (str.includes(ending)) {
    return true;
  }
  return false;
}

console.log(solution("football", "ball")); // true
console.log(solution("football", "foot")); // true
console.log(solution("sumo", "um")); // true

about 4 years ago · Juan Pablo Isaza Report

0

A more modern version (ES6+) of the function declaration would be this oneliner

const solution = (str1, str2) => str1.endsWith(str2);

If you need case insensitive testing. you can also construct a regular expression so you do not need to lowerCase.

Remove the "i" to leave it case sensitive

const solution = (str1, str2) => new RegExp(str2+"$","i").test(str1);

console.log(solution("football", "ball")); // true
console.log(solution("football", "Ball")); // true
console.log(solution("football", "foot")); // false
console.log(solution("sumo", "um")); // false

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!