- 掷骰子决定做什么(1点唱歌、2点学狗叫、3点念绕口令、……)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button id="bt" type="button">摇色子</button>
<p id='num'></p>
<script type="text/javascript">
let btn=document.querySelector('#bt')
btn.addEventListener('click',() => {
n=parseInt(Math.random()*6+1)
let num=document.querySelector('#num')
num.textContent=`你要出来的点数${n}`
switch(n){
case 1:
alert('唱歌')
break
case 2:
alert("学狗叫")
break
case 3:
alert('念绕口令')
break
case 4:
alert('跳钢管舞')
break
case 5:
alert("大喊三声我是傻逼")
break
case 6:
alert('恭喜没有惩罚')
break
}
})
</script>
</body>
</html>
-
在页面上打印各种三角形形状
1 )
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
let dic={1:"A",2:"B",3:"C",4:"D",5:"E"}
for(let i=1;i<6;i+=1){
document.write('<p>')
for ( let j=1;j<=i;j+=1)
{
document.write(dic[i])
}
document.write('</p>')
}
</script>
</body>
</html>
2 )
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
let dic={1:"A",2:"B",3:"C",4:"D",5:"E"}
for(let i=1;i<6;i+=1){
document.write('<p>')
for (let x=1;x<10-(i*2);x+=1){
document.write(" ")
}
for ( let j=1;j<=i*2-1;j+=1)
{
document.write(dic[i])
}
document.write('</p>')
}
</script>
</body>
</html>
3 )
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
let dic={1:"A",2:"B",3:"C",4:"D",5:"E"}
for(let i=1;i<6;i+=1){
document.write('<p>')
for (let x =1 ;x<5-i;x+=1)
{
document.write(" ")
}
for ( let j=1;j<=i*2-1;j+=1)
{
document.write(dic[i])
}
document.write('</p>')
}
</script>
</body>
</html>
4 )
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
let dic={1:"A",2:"B",3:"C",4:"D",5:"E"}
for(let i=1;i<6;i+=1){
document.write('<p>')
for (let x =1 ;x<i;x+=1)
{
document.write(" ")
}
for (let j=0;j<6-i;j+=1)
{
document.write(dic[i]+' ')
}
document.write('</p>')
}
</script>
</body>
</html>
- 在页面上输出二组数据,每行10个数字,两组数据用
分隔:
第一组:1-1000之间的质数
第二组:前20个斐波拉切数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function isnum(i){
n=parseInt(Math.sqrt(i))
for (let j=2;j<n+1;j+=1){
if(i%j==0){
return false
}
}
return true
}
document.write("1 2 ")
for(let i=3;i<1001;i+=1){
if(isnum(i)){
document.write(`${i} `)
}
}
document.write('<hr>')
function num1(n){
if (n==1||n==2){
return 1;
}
return num1(n-1)+num1(n-2)
}
for (let i=1;i<=20;i+=1){
document.write(num1(i)+' ')
}
</script>
</body>
</html>