冒泡排序、36選7不重複、水仙花數、九九乘法表等案例

九九乘法表數組

var str = '<table border=1px>';dom

         for(var i = 1;i <=9 ;i++ ){ui

                   str += '<tr>';排序

                   for(var j = 1;j <=9 ;j++ ){get

                            // str += "<td></td>"數學

                            if (i>=j) {it

                                     a = i*j;io

                                     str = str + '<td>' + i + '*' + j + '='+ (i*j)+ '</td>' ;console

                                     // document.write(a +'='+ j +'*'+ i);table

                            }

                   }

                   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);

}

相關文章
相關標籤/搜索