若字符串只包含空格,返回 TRUE,否则返回 FALSE
var has_space = function(s) {
var bool = true;
if (s === "") { // 先排除空字符串的情况
bool = false;
} else {
for (var i = 0; i < s.length; i++) {
if (s[i] !== " ") {
bool = false;
}
}
}
console.log(bool + ", \"" + s + "\"");
return bool;
}
has_space(' ');
has_space(' ');
has_space('hello');
has_space('Hello World');
has_space('');
得到的结果:
true, " "
true, " "
false, "hello"
false, "Hello World"
false, ""