0一、屏幕打印2000到3000之間的全部的數。javascript
<script type="text/javascript"> for (var i = 2000; i < 3001; i++){ document.write(i+"<br />") } </script>
0二、求450到550之間全部奇數的和。java
<script type="text/javascript"> var num = 0; for (var i = 451; i < 550; i+=2) { num += i } document.write("450到550之間全部奇數的和爲:" + num) </script>
0三、找出200之內,既能整除3又能整除5的全部數。app
<script type="text/javascript"> for (var i = 0; i < 201; i++) { if(i % 3 == 0 && i % 5 == 0){ document.write(i+"<br />") } } </script>
0四、若是輸入一個數,則彈出對話框「您輸入的是偶數」,不然彈出「您輸入的是奇數」spa
<script type="text/javascript"> var oBtn = document.getElementById("btn") oBtn.onclick = function(){ var oInputVal = parseInt(document.getElementById("num").value) if(oInputVal % 2 == 0){ alert("您輸入的是偶數") }else{ alert("您輸入的是奇數") } } </script>
0五、頁面彈出輸入框,只有當用戶輸入Alice和Bob這兩個名字時,纔會向用戶問好「你好」。code
<script type="text/javascript"> var name = prompt("請輸入你的姓名") if(name == "Alice" || name == "Bob"){ alert("你好") } </script>
0六、前後彈出三個輸入框,第一個提示:輸入一個數字,第二個提示:輸入一個+-*/中的任意運算符,第三個提示:輸入一個數字,最後彈出他們的運算結果。blog
<script type="text/javascript"> var one = parseInt(prompt("請輸入一個數字")); var two = prompt("請輸入一個運算符"); var three = parseInt(prompt("請輸入一個數字")); var num; if(two == "+"){ num = one + three }else if(two == "-"){ num = one - three }else if(two == "*"){ num = one * three }else if(two == "/"){ num = one / three } alert(num) </script>
0七、屏幕打印1到1000全部能整除11而且不能整除17的數的和。three
<script type="text/javascript"> var num = 0; for(var i = 0 ; i < 1001 ; i++){ if(i % 11 == 0 && i % 17 != 0){ num += i } } document.write(num) </script>
0八、寫程序屏幕打印出x,y的值(正整數):x+y=10,3x+4y=36。ip
<script type="text/javascript"> for (var x = 1; x <= 10; x++) { for (var y = 1; y <= 10; y++) { if (x + y === 10 && 3 * x + 4 * y === 36) { document.write("x的值爲" + x + '<br>' + "y的值爲" + y) } } } </script>
0九、如何利用JS生成一個table?行和列由用戶本身輸入決定。get
<body> <p>請輸入行與列</p> <input type="text" id="row" />行<br /> <input type="text" id="column" />列<br /> <input type="button" value="建立" id="btn"> <div class="box" id="box"></div> <script type="text/javascript"> var oBtn = document.getElementById("btn"); var oBox = document.getElementById("box"); oBtn.onclick = function(){ var oRow = parseInt(document.getElementById("row").value); var oColumn = parseInt(document.getElementById("column").value); var oTable = document.createElement('table'); oTable.setAttribute('border',"1"); for (var i = 1; i <= oRow; i++) { oTr = document.createElement('tr'); for(var a = 1 ; a <= oColumn ; a++){ oTd = document.createElement('td'); var oText = document.createTextNode(i+"行"+a+"列") oTd.appendChild(oText) oTr.appendChild(oTd) } oTable.appendChild(oTr) } oBox.appendChild(oTable) } </script> </body>