js计算100以内所有奇数的和
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
//计算100以内所有奇数的和
window.onload = function(){
var h = document.getElementById('h');
var hh = document.getElementById('hh');
var sum = 0;
for(var i = 1; i < 100; i++){ //第一中方法
if(i % 2 == 0){
// console.log(i);
continue;
}
// console.log(i);
sum += i;
}
// alert(sum);
h.innerHTML = '100以内所有奇数和为:' + [sum];
var sum2 = 0;
for(var j = 1; j <= 100; j=j+2){
//console.log(j);
sum2 += j;
}
hh.innerHTML = '100以内所有奇数和为:' + [sum2];
}
</script>
</head>
<body>
<h2 id="h"></h2>
<h2 id="hh"></h2>
</body>
</html>
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17634847.html