Basic Algorithm Scripting - Confirm the Ending

Tell us what’s happening:
Hi, I tried writing for this challenge with spreading the strings into arrays. Passes almost every test, but fails the “confirmEnding(“Open sesame”, “sage”) should return false” test. Here’s an extract from the console.log:
e e
m m
a a
s s
fits
e e
m g
a a
s s
fits
e e
m m
a a
s g
doesn’t fit

I know I could’ve done it more simply, but I’m still wondering what could cause the one test to fail.

  **Your code so far**
function confirmEnding(str, target) {

let sarray = [...str];
let tarray = [...target];
let x = 0;
let y = 0;

for (let i = 1; i <= target.length; i++) {
  x = sarray[sarray.length - i];
  y = tarray[tarray.length - i];
  console.log(x, y);
};
if (x === y) {
  console.log("fits");
    return true;
  }
else {
  console.log("doesn't fit");
  return false;
  };
}

confirmEnding("Bastian", "ian");
  **Your browser information:**

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

Challenge: Basic Algorithm Scripting - Confirm the Ending

Link to the challenge:

If you are going for looping here, you should do some checks inside the loop I assume?

When you are comparing x and y after all loop ended - what exactly are you comparing? think about that.

Yeah, sure, it can be done with one line of code using .slice() for example.

Your code is just comparing last alphabet of both string, log for fails case you will get it …

I moved the comparison inside the for loop , added a variable to have a final comparison outside the loop, and included a return false to stop the loop the moment a comparison fails. Works now, thanks!

function confirmEnding(str, target) {

let sarray = […str];
let tarray = […target];
let x = 0;
let y = 0;
let c = 0;

for (let i = 1; i <= target.length; i++) {
x = sarray[sarray.length - i];
y = tarray[tarray.length - i];
console.log(x, y);
if (x === y) {
console.log(“fits”);
c = 1;
}
else {
console.log(“doesn’t fit”);
return false;
};
}
if (c === 1) {
console.log(“good”);
return true;
};
};

confirmEnding(“Bastian”, “ian”);

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