在这里,我试图创建一个程序,计算字符串中的所有单词,但它给了我一个错误的答案.请看看这个:
let text = "Hello world ";
const mystring = text.split(" "); // output = "Hello", "world", " ", " "
console.log(mystring);
var countwords = 0
mystring.forEach((element) => {
if(element != " "){
countwords = countwords + 1
}
})
console.log(countwords);
我试图用js创建一个程序,计算字符串中的所有单词,但它给了我错误的答案.输出不应该是2,而是显示4.