JS Basic Three

Source: Internet
Author: User

Enter a number to determine if the number is prime

var a = + prompt (' Please enter a number ')

If (a = 1) {

Console.log (a + "is prime number");

}else {

For (var I =2;i < A;i++) {

  if (a% i = = 0) {
  console.log (a + ' not prime number ')
  }else {
  Console.log (a + ' is prime number ')
  }
}

}

All prime numbers within the output 200

for (var i = 1; i<=200; i++) {    //determine If I is prime number    //first assume I is prime number    IsPrime = true;    For (var J=2;j<=i-1;j++) {        if (i% j = = 0) {           IsPrime = false;            Break        }    }    if (isprime) {        console.log (i)    }}}

<!--output 99 Multiplication table-

For (i=1;i<=9;i++) {

For (j+i;j<=9;j++) {console.log (i + ' * ' + j + ' = ' + i * j)

}}

<!--series Bubble Sort--compare The largest of the first two numbers to the right, and the second and third numbers to the right; always put the largest one on the right <!--sequence from small to large var arr = [2,3,4,2,3]for (var i = 0; i < arr.length-1; i++) {the length here represents the size of the array, that is, several items, and the middle arr.length-1 represents the number of moves /c3>

Move the largest item in the first length-i of the array to the Rightmost//for (var J =0; J < arr.length-i-1; j + +) {if (arr[j] > Arr[j +1];    Create a temporary variable to save the value of the previous arr[j] temp is a temp variable var temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}

Console.log (arr);

Enter an array to output the number of this number in the order from small to large var arr = []; <!--how many numbers to enter--var inputnumber = 10; The number of this array is ten for(var I =0;i < inputnumber;i++) {

arr[i] = + Prompt (' Please enter ' + (i + 1) + ' number ');

}

for (var i = 0; i < arr.length-1; i++) {

for (var j = 0;j < arr.length-i-1; j + +) {if (arr[j] > arr[j +1]) {var temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = Temp

}}}<!--console.log (' input numbers from small to large: ');--var str = ' input numbers, from small to large: \ n '; for (var i = 0; i < arr.length; i++) {

str = str + arr[i] + ';

}

Console.log (str);

Functions: Enhanced code readability function sayHello () {

Console.log (hello); Console.log (js);

}SayHello (); reference function SayHello (); Here The result is Helle JS

When the parameter function is defined, the parameter defined, called the parameter function at the time of invocation, is called the arguments passed in as argument functions SayHello (name) {

Console.log (hello); Console.log (name);

}SayHello (' World '); SayHello (' js ');

function add (a, b) {you can use a space after a comma to write nicely, this is the formal parameter

Console.log (a + b);

}

Add (10,20); the argument add (10,x) Here is 10, is a constant number, x is a variable ' hello ' for the string hello, the literal will always represent a meaning, literal constant: is always unique

The order of execution of the function: 1, the value of thecalculated argument takes the value after the final calculation, as the data of the incoming function

2, the system automatically to declare the formal parameters

3, The function statement block inside the content (more formal parameters than the number of parameters, more than the shape of the participation show undefined argument more than formal parameters, many arguments do not display,)var x = ten; var y = 20; function Add (a, b) {

The system automatically declares the Parameter//var a = 1 + x Error//var a = 20;var B = 2

}var

JS Basic Three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.