function writeDocument(variable){
document.write(variable+"<br/>");
}
// 1.统计该字符串"Rome was not built in a day"中字符的个数,不允许使用字符串对象的length属性
var str="Rome was not built in a day"; //定义
arr =str.replace(/\s*/g,""); //正则表达式,去掉所有的空格
var sum = 0; //统计初始化
for(var i of arr)
{
sum++;
}
writeDocument("字符个数为:"+sum);
writeDocument("<br/>")
// 2.字符串中的字符顺序颠倒,如给你"abcde",你要得到"edcba"。
var str = "abcde";
for(var i=4;i>=0;i--)
{
result=str[i];
document.write(result);
}
writeDocument("<br/>")
// 3.利用3种循环来计算1+2+3+…+100的值
//for,while,do...while
//for循环
var i;
var sum=0;
for(i=0;i<=100;i++)
{
sum+=i;
}
writeDocument("for循环:")
document.write(sum);
writeDocument("<br/>")
//while循环
var i=1;
var sum=0;
while(i<=100)
{
sum+=i;
i++;
}
writeDocument("while循环:")
document.write(sum);
writeDocument("<br/>")
//do....while
var sum=0;
var i = 1;
do{
sum = sum +i;
i++;
}while(i<=100);
writeDocument("do...while循环:")
document.write(sum);
console