📜  确认结束 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:33.045000             🧑  作者: Mango

代码示例1
// Confirm the Ending

// Check if a string (first argument, str) ends with the given target string (second argument, target).
// This challenge can be solved with the .endsWith() method, which was introduced in ES2015.
// But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
function confirmEnding(str, target) {
    let endOfStr = str.slice(str.length - target.length);

    return endOfStr === target;
}

confirmEnding('Bastian', 'an');

// OR

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

confirmEnding('Bastian', 'n');