js beginner Classic title
1. According to a digital date, this date is determined, for example, the day of the year: 2016 and 02 and 11, the calculation result is 42
1 var y = 2016;//+prompt("请输入年份") ; 2 var m = 2; 3 var day = 11; 4 var aDays = 0; 5 switch (m) { 6 case 12: 7 aDays += 30; 8 case 11: 9 aDays += 31; 10 case 10: 11 aDays += 30; 12 case 9: 13 aDays += 31; 14 case 8: 15 aDays += 31; 16 case 7: 17 aDays += 30; 18 case 6: 19 aDays += 31; 20 case 5: 21 aDays += 30; . 4 Case 22 is: = + 31 is aDays 23 is; 24. 3 Case: 25 IF (! Y. 4 == 0 &&% Y = 0% 100% 400 || Y == 0) {// Analyzing February 29 days or 28 days; 26 is aDays = 29 +; 27} the else { 28 aDays + = 28; 29} 30 Case 2: 31 is 31 is aDays + =; 32. 1 Case: 33 is aDays + = Day; 34 is BREAK; 35 default: 36 the console.log ( "error month ") 37 [BREAK; 38 is 39} 40 the console.log (aDays);
2: obtaining 1/1 - 1/4 ... ..1 / 100 - 1/2 + 1/3
SUM = 0 var; var A =. 1; for (i = var. 1; i <= 100; i ++) { IF (i% 2 == 0) {// if i is even, then use - SUM = SUM - A / I; } the else { SUM = SUM + A / I; // otherwise adding } } the console.log (SUM);
3. basketball fell from 5 meters high, bounce every time the height of the original 30%, after several bounce, basketball height less than 0.1 meters.
Var. 5 = H. 1; 2 = I var. 1; . 3 the while (to true) { . 4 H H * = 0.3; . 5 IF (H <0.1) { . 6 the console.log ( "bounced" + i + "views") ; . 7 bREAK; // if i is less than 0.1, the interrupt cycle; . 8} . 9 i ++; 10}
4. Print the 1000--2000 all the leap years, and outputs (document.write) in the form of four per row number
N-var = 0. 1; 2 for (var I = 1000; I <= 2000; I ++) { . 3 IF (I. 4 == 0 &&% I = 0% 100% 400 I == 0 ||!) { . 4 Document .write (I + "& nbsp; & nbsp; & nbsp;"); . 5 n-++; // record the number of times, incrementing 6 if (n% 4 == 0 ) {// when a multiple of 4, the wrap; . 7 document.write ( "<br>"); . 8} . 9} 10}
5. Print right triangle
1 for(var j = 1; j <=6; j++){ 2 for(var i = 1; i <= j; i++){ 3 document.write("*"); 4 } 5 document.write("<br>"); 6 }
6. Print equilateral triangle
// split into three triangles, right triangle is reversed in a first, a second equilateral triangle, a triangle with a space to squeeze out.
1 for(var i=1; i <=7; i++){ 2 for(var a = 7; a >i; a--){ 3 document.write(" "); 4 } 5 for(var k = 1; k<i+3; k++){ //加三则按照3 5 7排列;不加则按照 2 4 6排列; 6 document.write("*"); 7 } 8 for(var k = 1; k<i; k++){ 9 document.write("*"); 10 } 11 document.write("<br>"); 12 }
7. Calculate the factorial // 10 * 1 * 2 * 3 * 4. . . * 10
1 var cj = 1; 2 for(var i = 1; i <= 10; i++){ 3 //console.log(i); 4 cj = cj * i ; 5 } 6 console.log(cj);
8. daffodils number between 100-999 requirements // abc = a * a * a + b * b * b + c * c * c is the number of daffodils
For. 1 (I var = 100; I <= 999; I ++) { 2 the parseInt var A = (I / 100); // Get the hundreds digit 3 var b = parseInt (i / 10)% 10; // Get ten bits 4 var c = parseInt (i% 10); // Get a digit . 5 IF (I == A * A * A + B * B * B + C * C * C) { . 6 the console.log (I ); 7} 8}
9. There is a board with 64 squares, sesame put a weight on the first box which is 0.00001kg, which put the second 2, the third inside with 4, put on the board all sesame by weight of a nested do loop;
//law:
//. 1. 1 2 *. 1 // 2 . 3. 1 * 2 * 2 // . 4 //. 1 * 2 * 2 * 2 . 5 var SUM = 0; . 6 for (var. 1 = K; K <= 64; K ++) { NUM = var. 1. 7; . 8 for (var. 1 = I; I <64; I ++) { . 9 NUM = NUM * 2;; // accordance with the law, each multiplied by 2 10} . 11 SUM = SUM + NUM; // every time the previous sum for adding; 12 is} 13 is the console.log (sum * 0.00001);
10. There is a small to large sorted array. Now enter a number, required by the laws of the original insert it into the array
/// The first, direct addition, for sorting Sort; function INSERT (ARR, Data) { arr.push (Data); arr.sort (function (A, B) { return A - B; }) }
var arr = [3, 4, 5, 7, 8, 9];
console.log(insert(arr, data))
// 1 second for (var I = 0; I <arr.length; I ++) { 2 IF (Data <ARR [I]) { . 3 BREAK; . 4} . 5} . 6 2. // this position to the end data, all again after a move . 7 for (var J = arr.length -. 1; J> = I; J,) { . 8 ARR [J +. 1] = ARR [J]; . 9} 10 // 3. the data to be inserted, at a position on the first step in finding . 11 ARR [I] = data; 12 is} 13 is ARR = var [. 3,. 4,. 5,. 7,. 8,. 9]; 14 the console.log (ARR ); 15 INSERT (ARR,. 6); 16 the console.log (ARR);
A defined in claim 11. Each array 30, the array is a random number from 1 to 10, every 5 digits, the number average value of five
// define an array var ARR = []; for (var I = 0; I <30; I ++) { arr.push (Random (1,10)); } the console.log (ARR); for (var L = 0; L <arr.length; + L =. 5) { the console.log (arr.slice (L, L +. 5)); the console.log (AVG (arr.slice (L, L +. 5))); } / / averaging function AVG (myArr with) { var SUM = 0; for (var J = 0; J <myArr.length; J ++) { SUM + = myArr with [J]; } return SUM /myArr.length; } // generates a random number function random (max, min) { return Math.round (Math.random () * (max-min)) + min; }
13. The write function generates 4 digital codes and generating 10 times, while the results into an array
function rnumber(){ var arr= []; for(var i = 0; i<10; i++){ var a = random(0,9); var b = random(0,9); var c = random(0,9); var d = random(0,9); arr.push("" + a + b + c + d); } return arr; } console.log(rnumber()); //生成随机数 function random(max,min){ return Math.round(Math.random() * (max-min)) + min; }
14. Sensitive word filtering // code problems! ! , Not yet resolved
var arr = ["a", "w", "e", "sv", "qw"]; var str = "aw暖哒e阿大阿sv,请问我去qw"; for (var i = 0; i < arr.length; i++) { for (var j = 0; j < str.length; j++) { str = str.replace(arr[i], "*"); } } console.log(str);
15. count the number of each character appearing showed {a: 2, b: 1, c: 2, d: 1}
STR = var "aabccdsdadadadsadadafqdqeqwsqwq"; // define an empty character var newStr = ""; // Loop through STR for (var I = 0; I <str.length; I ++) { // If there is no null character str [i ], it will return -1, IF (newStr.indexOf (STR [I]) == -1) { // null character and placed in newStr + STR = [I]; } } the console.log (newStr) ; // get the array to heavy characters
newobj = {} var; for (var I = 0; I <str.length; I ++) { IF (newobj [STR [I]]) {// first time is undefined, so take else, is 1 second and take the first one. If there newStr str [i], then ++, or newobj [STR [I]] ++; } the else { newobj [STR [I]] =. 1; } } the console.log (newobj);
16. The randomly generated within a five-digit number, and then outputs the total number of how many and what each is respectively
var fiveNumber = random(0, 99999); var fiveString = fiveNumber.toString(); console.log("该数是" + fiveString); console.log("该数共有" + (fiveString.length) + "位"); for (var i = 0; i < fiveString.length; i++) { numI = parseInt(i); console.log("第" + (numI + 1) + "位是" + fiveString.charAt(i)); // console.log(fiveString.charAt(i)) } function random(max, min) { return Math.round(Math.random() * (max - min)) + min; }