0034【Edabit ★☆☆☆☆☆】【修改Bug4】Buggy Code (Part 4)

0034【Edabit ★☆☆☆☆☆】【修改Bug4】Buggy Code (Part 4)

bugs conditions strings

Instructions

Emmy has written a function that returns a greeting to users. However, she’s in love with Mubashir, and would like to greet him slightly differently. She added a special case in her function, but she made a mistake.

Can you help her?

Examples
greeting("Matt") // "Hello, Matt!"
greeting("Helen") // "Hello, Helen!"
greeting("Mubashir") // "Hello, my Love!"
Notes
  • READ EVERY WORD CAREFULLY, CHARACTER BY CHARACTER!
  • Don’t overthink this challenge; it’s not supposed to be hard.
Solutions
// bug solution
function greeting(name) {
    return "Hello, " + name + "!";  // bugs here
    if(name == "Mubashir") {
        return "Hello, my Love!";
    }
}
// correct it !! as below
function greeting(name) {
    if(name == "Mubashir") {
        return "Hello, my Love!";
    }
    return "Hello, " + name + "!"; 
}
TestCases
let Test = (function(){
    return {
        assertEquals:function(actual,expected){
            if(actual !== expected){
                let errorMsg = `actual is ${actual},${expected} is expected`;
                throw new Error(errorMsg);
            }
        }
    }
})();

Test.assertEquals(greeting("Matt"), "Hello, Matt!")
Test.assertEquals(greeting("Helen"), "Hello, Helen!")
Test.assertEquals(greeting("Mubashir"), "Hello, my Love!")

你可能感兴趣的:(#,Edabit,javascript,算法)