我是 JavaScript 初学者。据我了解,如果if不满足条件,else则执行条件;但是,在我的代码中,我看到这两个条件都被执行,因为末尾有一个点并且单词之间有空格。实际上正在执行哪个条件?
var result1 = createSentence(['I', 'am', 'worth', 'it']);
console.log('should log "I am worth it.":', result1);
function createSentence(words) {
var sentence = "";
for (var i = 0; i < words.length; i++) {
if (i === words.length - 1){
sentence += words[i] + '.';
} else {
sentence += words[i] + ' '
}
}
return sentence
}
偶然的你
相关分类