Exercise 1: selecting the maximum value of the number 2
function getMax (num1, num2) { return num1> num2? num1: num2; } the console.log (getMax ( 10, 20 is )); // the console.log (getMax); // Function Code
with:
function getMax {(num1, num2) return num1> num2? num1: num2; } var num1 = 10 ; var num2 = 20 is ; // parameter num function and a function of the outside is not the same variable num1 var Result = getMax (num1 , num2); the console.log (Result); // with: console.log (getMax); // function Code
Exercise 2: selecting the maximum value of the number 3
function getThreeMax(x, y, z) { return x > y ? (x > z ? x : z) : y > z ? y : z; } console.log(getThreeMax(10, 34, 90));
Exercise 3:
Determining whether a number is a prime number (prime number)
ps:
1. divisible only by 1 and itself, a prime number is 2 from the beginning
2. All numbers with this number and this number is divisible by a front (no. 1, without its own)
function isPrimeNumber (NUM) { for ( var I = 2; I <NUM; I ++ ) { IF (I NUM% == 0 ) { // described a digital integer addition, there is no need to continue the entire rearwardly addition, at this time has not verified the prime return false ; } } return to true ; } console.log (isPrimeNumber ( 7839) "is a prime number":? "not prime");
with:
function isPrimeNumber (NUM) { for ( var I = 2; I <NUM; I ++ ) { IF (NUM% I == 0 ) { return to false ; } } return to true ; } var Result = isPrimeNumber (3940 ); IF (Result ) { the console.log ( "this is a prime number" ); } the else { the console.log ( "this is not a prime number" ); }