//字符倒序 function countNumbers(str) { var sum = 0; //字符串去空格 str = str.replace(/\s*/g,""); // 分割字符串为单字符数组 var arr = str.split(""); // 遍历数组 for(var i in arr) { sum++; } return sum; } var stringValue = "Rome was not built in a day"; document.write(countNumbers(stringValue));