0048【Edabit ★☆☆☆☆☆】【字符串拼接】Concatenate First and Last Name into One String

0048【Edabit ★☆☆☆☆☆】【字符串拼接】Concatenate First and Last Name into One String

formatting language_fundamentals strings

Instructions

Given two strings, firstName and lastName, return a single string in the format “last, first”.

Examples
concatName("First", "Last") // "Last, First"
concatName("John", "Doe") // "Doe, John"
concatName("Mary", "Jane") // "Jane, Mary"
Notes
  • Don’t forget to return the result.
Solutions
function concatName(firstName, lastName) {
    return lastName.concat(", ").concat(firstName);
}
TestCases
let Test = (function(){
    return {
        assertEquals:function(actual,expected){
            if(actual !== expected){
                let errorMsg = `actual is ${actual},${expected} is expected`;
                throw new Error(errorMsg);
            }
        },
        assertSimilar:function(actual,expected){
            if(actual.length != expected.length){
                throw new Error(`length is not equals, ${actual},${expected}`);
            }
            for(let a of actual){
                if(!expected.includes(a)){
                    throw new Error(`missing ${a}`);
                }
            }
        }
    }
})();

Test.assertEquals(concatName("John", "Doe"), "Doe, John")
Test.assertEquals(concatName("First", "Last"), "Last, First")
Test.assertEquals(concatName("A", "B"), "B, A")

// In case someone is making odd assumptions about comma characters.
Test.assertEquals(concatName(",", ","), ",, ,")

你可能感兴趣的:(#,Edabit,java,前端,javascript)