This is my approach after seeing the answers i think my approach was much longer

  const endingWordLength=target.split("").reverse();
  let spellChecker;
  let isEnding;
  if(str.split(" ").length===1){
    // which means the "str" is one word;
    const newArr=str.split("");
    spellChecker=newArr.reverse();
  }else if(str.split(" ").length>1){
    // Which means the "str" is more than one word;
    const newArr=str.replaceAll(" ","").split("");
    spellChecker=newArr.reverse();
  }
  for(let i=0;i<endingWordLength.length;i++){ 
    if(spellChecker[i]===endingWordLength[i]){
       isEnding=true;
    }else{
       isEnding=false
       break;
    }
  }
  return isEnding;
}

confirmEnding("Open sesame","sage");