SOURCE

/*如果有一个字符串"Rome was not built in a day",
请用程序统计该字符串中字符的个数,
不允许使用字符串对象的length属性。
*/

//解1
var str='Rome was not built in a day'

str=str.replace(/ /g,"")

console.log(str)

var sum=0

for(var i of str){

sum++

}

console.log(sum)



//解2
var str='Rome was not built in a day'

str=str.replace(/ /g,"c")

console.log(str)

var sum=0

for(var i of str){
if(i=="c"){
    sum=sum-1;
}
sum++

}

console.log(sum)


console 命令行工具 X clear

                    
>
console