javascript - Syntax error when trying to test for empty string -
in javascript, trying simple task of testing , returning longest of 2 words. if words empty return should "empty string". keep getting syntax error when testing empty string or elseif statement. using (!word1) , (!word2) because understanding in javascript boolean statement , should false. please tell me going wrong:
function longest(word1, word2) { if (word1.length >= word2.length) { return (word1); } else { return (word2); } else if (!word1) || (!word2) { return "an empty string"; } } console.log(longest('hi'));
if
conditions should surrounded 1 pair of parentheses.
change else if (!word1) || (!word2)
else if (!word1 || !word2)
.
Comments
Post a Comment