JS 算法基础题
1.设计一个弹框计算器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>弹框计算器</title>
</head>
<body>
<script type="text/javascript">
var a = prompt("请输入您要计算的内容");
var b=eval(a);
document.write(b)
</script>
</body>
</html>
2.打印九九乘法表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>九九乘法表</title>
</head>
<body>
<script>
for(var i=1;i<=9;i++){
for(var j=1;j<=i;j++){
document.write(i+"x"+j+"="+i*j+" "+" ")
}
document.write("<br>")
}
</script>
</body>
</html>
3.价格打折:请用户输入消费金额,如果用户消费的金额>=100元,则享受8折优惠,控制台显示实际应收金额
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>价格打折</title>
</head>
<body>
<script>
var a=prompt("请输入消费金额:");
if(a>=100){
document.write("实际实收金额为:"+0.8*a+"元");
}
else{
document.write("实际实收金额为:"+a+"元");
}
</script>
</body>
</html>
4.打印国际象棋棋盘(8*8循环完成)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>国际象棋棋盘</title>
<style>
.d1{
width: 800px;
height: 800px;
border: 1px solid aqua;
}
.d2{
width: 100px;
height: 100px;
background-color:black;
float: left;
}
.d3{
width: 100px;
height: 100px;
background-color: wheat;
float: left;
}
</style>
</head>
<body>
<div class="d1">
<!-- <div class="d2"></div>
<div class="d3"></div> -->
<script>
for(var i=1;i<=8;i++){
for(var j=1;j<=8;j++){
if((i+j)%2==0){
document.write('<div class="d2"></div>');
}
else{
document.write('<div class="d3"></div>')
}
}
}
</script>
</body>
</html>
5.接收用户输入的数字,判断是否是素数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>判断素数</title>
</head>
<body>
<script>
var a=prompt("请输入一个正整数:");
var c=0;
for(var b=1;b<=a;b++){
if(a%b==0){
c++;
}
}
if(c==2){
document.write(a+"是素数")
}
else{
document.write(a+"不是素数")
}
</script>
</body>
</html>
6.计算100以内所有奇数的和.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var sum=0;
for(var i=1;i<100;i+=2)
{
sum+=i;
}
document.write("100以内所有奇数和为:"+sum);
</script>
</body>
</html>
7.计算100以内的所有数的和
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var sum = 0;
for(var i = 1; i <= 100; i++){
sum = sum + i;
}
alert(sum);
</script>
</body>
</html>
8.输入一个四位数,在控制台分别显示个位,十位,百位,千位的数值
var a = prompt("请输入一个四位数的正整数");
var b = parseInt(a/1000);
var c = parseInt(a/100);
var d = c%10;
var e = a%100;
var f = parseInt(e/10);
var g = a%10;
console.log('千位数是:' + b,'百位数是:' + d,'十位数是:' + f,'个位数是:' + g);