用console.log把从1到N(N=100)依次输出到每一行,其中有些数字被特殊替换:
15的倍数替换为helloworld,
3的倍数(非5的倍数)替换为hello,
5的倍数(非3的倍数)替换为world

———–

<script>
//普通版的
var str="";
for(var i=1;i<=100;i++){
if(i%5!==0 && i%3===0){
str+="hello";
}else if(i%5===0 && i%3!==0){
str+="world";
}else{
str+=i;
}
}
console.log(str);
</script>

—–装X版—-

<script>
var str = "";
for (var i = 1; i <= 100; i++) {
str += " " + i + " ";
}
str = str.replace(/(?:^| *)(\d+)(?: *|$)/g, function () {
var val = RegExp.$1;
return val % 5 === 0 && val % 3 !== 0 ? " word " : (val % 5 !== 0 && val % 3 === 0 ? " hello " : " " + val + " ");
});
console.log(str);
</script>

—-

<script>
var str = "";//声明一个空的字符串;
for (var i = 1; i <= 100; i++) {
str += " " + i + " ";//str=str+ " " + i + " ",自增,并且出来的字符串中,用空格隔开;
}

str = str.replace(/(?:^| *)(\d+)(?: *|$)/g, function () {
//上面是正则;
var val = RegExp.$1;
//声明一个正则;

return val % 5 === 0 && val % 3 !== 0 ? " word " : (val % 5 !== 0 && val % 3 === 0 ? " hello " : " " + val + " ");
//返回结果: 如果:var%5,绝对比较后,余数是0,并且%3的余数不等于0,
//返回:“word”字符串,
//否则返回以下:
//如果:var%5,比较后,余数不是0,并且%3的余数等于0,
//返回:“hello”
//否则执行以下结果:
// " " + val + " "
});
console.log(str);//控制台出来结果;
</script>