九九乘法表
var str = '<table border=1px>';
for(var i = 1;i <=9 ;i++ ){
str += '<tr>';
for(var j = 1;j <=9 ;j++ ){
// str += "<td></td>"
if (i>=j) {
a = i*j;
str = str + '<td>' + i + '*' + j + '='+ (i*j)+ '</td>' ;
// document.write(a +'='+ j +'*'+ i);
}
}
str = str + '</tr>';
document.write('<br>');
}
str += '</table>';
document.write(str);
纸的厚度
var a = 0.00001;
var sum = 0;
for (i = 1; i<=30;i++){
// sum = sum + 2*a;
a = 2*a;
}
document.write(a +'<br>'+'米');
var a = 0.00001;
var sum = 0;
for (i = 1; i<=30;i++){
// sum = sum + 2*a;
a = 2*a;
if (a >=1) {
document.write('对折'+i+'次超过1米');
break;
}
}
36个数选7个,不重复
var arr = [];
var getNum = [];//存放取出来的七个数
for(var i = 0; i < 36;i++){
// arr[i] = i+1;
arr.push(i+1);
}
for (i = 0; i < 7; i++){
var index = parseInt(Math.random()*36;
var flag = true;//假设没有重复的数字,可以放入。
// console.log(arr[index]);
//不重 往里放和之前判断里面有没有相同的
for(var k in getNum){
if(getNum[k] == arr[index]){
flag = false;
break;
}
}
if(flag){
getNum.push(arr[index]);
}
else{
i--;
}
}
console.log(getNum);
水仙花数
for(i = 100;i < 1000;i++){
// 百位
a = parseInt(i/100);
// 十位
b = parseInt(i/10)%10;
// 个位
c = i%10;
if (a*a*a+b*b*b+c*c*c == i) {
// alert(i +"是水仙花数");
document.write(i +"是水仙花数"+'<br>')
}
else{
// alert(i +"不是水仙花数");
}
}
//六个整数
function sixnum(){
var arr1 = [11,51,65,12,78,32];
var sum = 0;
// var con = 0;
for(var i = 0;i < arr1.length;i++){
var sum = (sum + arr1[i]);
// con++;
}
// var avg = sum / con;
document.write(sum/(arr1.length));
}
// 七个整数
function sevennum(){
var arr1 = [11,51,65,12,78,32,56];
var max = arr1[0];
var min = arr1[0];
var max_x = 0 ;
var min_x = 0 ;
for(var i = 0 ; i < arr1.length ; i++){
// var x = arr1[i];
if (arr1[i]>max) {
max = arr1[i];
max_x = i ;
}
if (arr1[i]<min) {
min = arr1[i];
min_x = i ;
}
}
document.write('最大值是'+ max + '下标是' +max_x + '<br>');
document.write('最小值是'+ min + '下标是' +min_x);
// arr.indexOf(max);找此数在数组中的位置。
}
// 九个整数
function ninenum(){
var arr1 = [2,3,9,6,2,4,7,3,6];
var sum = 0;
var con = 0;
for(var i = 0 ; i < arr1.length ; i++){
if (arr1[i]%2 != 0){
sum = sum + arr1[i];
con ++;
}
var avg = sum / con;
}
document.write('奇数的平均数是' + avg +'');
}
// 评委
function pingwei(){
var arr1 =[9,8,4,6,7,1,2,3,2,5];
var max = arr1[0];
var min = arr1[0];
var max_x = 0 ;
var min_x = 0 ;
var sum = 0;
for(var i = 0 ; i < arr1.length ; i++){
// var x = arr1[i];
sum = sum + arr1[i] ;
if (arr1[i]>max) {
max = arr1[i];
max_x = i ;
}
if (arr1[i]<min) {
min = arr1[i];
min_x = i ;
}
}
// var avg = sum / 8;
document.write('去掉的最大值是'+ max + '下标是' +max_x + '<br>');
document.write('去掉的最小值是'+ min + '下标是' +min_x + '<br>');
document.write('平均分是' + ((sum - max - min)/(arr1.length-2)));
}
// 四个随机数
function suijishu(){
var str = '';
var arr = [1,2,3,4,5,'a','b','c','d'];
for(var i = 0; i < 4; i++){
var index = parseInt(Math.random()*arr.length);
str += arr[index];
}
console.log(str);
}
//六个人学号、成绩
function classgoo(){
var arr = [];
var classname = ['语文成绩','数学成绩','英语成绩'];
for ( var i = 0 ; i < 2;i++){
var name = prompt('请输入第'+ (i+1)+ '个名字');
var brr = [name]; //存放每一个学生的信息
for( var k in classname){
brr[+k+1] = +prompt('请输入'+ name +'的'+ classname[k]);
// console.log(brr);
}
arr[i] = brr;
}
console.log(arr);
for (var i in arr){
var sum = 0;
for(var k in classname){
sum += +arr[i][+k+1];
// arr[i][classname.length+1]
// arr[i][arr[i].length] +=
}
arr[i][arr[i].length] = sum;
arr[i][arr[i].length] = sum/(classname.length);
}
console.log(arr);
}
// for (k = 0 ; k < shuzu changdu ; k++)
三替换3的倍数,五替换5的倍数,三五替换既是3又是5的倍数。
for(var i = 1 ; i <=100 ; i++){
if (i%3 == 0 && i%5 == 0) {
document.write('三五' + '<br>');
}
else if (i%5 == 0) {
document.write('五' + '<br>');
}
else if (i%3 == 0) {
document.write('三' + '<br>');
}
else {
document.write(i + '<br>');
}
}
冒泡排序
function maopao(){
var arr = [5,8,2,9,3,7,1];
for (var i = 0 ;i < arr.length;i ++ ){
for (var k = 0; k< arr.length -1 -i;k++){
if (arr[k] > arr[k+1]) {
var temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}
console.log(arr);
}