Some test questions encapsulated by functions in JS (including loop problems)

1. Write a function to calculate the sum, difference, product, and quotient of two numbers

  • Idea: first define a function, both declarative and assignment, and then give 3 formal parameters to receive num1, num2, addition, subtraction, multiplication and division respectively, and then use switch to judge, when it means 2 b为1numbers 相加, When b为2it means 2 numbers 相减, b为3when it means 2 numbers 相乘, when b为4it means 2 numbers 相除.
function a(num1,num2,b){
     switch (b){
        case 1:
       		return num1+num2;
            break;
       case 2:
        	return num1-num2;
       		break;
       case 3:
        	return num1*num2;
        	break;
        case 4:
        	return num1/num2;
       		break;
       }
}
 alert(a(1,2,1))

2. Write a function to calculate the size of three numbers and output them in ascending order

1. Method 1 (Basic)

  • Idea: Let’s assume that the first number is the smallest, define it minto store the minimum value with subscript 0, and then use the for loop to compare one by one. If there is a number smaller than this number, assign the smaller number to it min, and then Define one maxto store the maximum value, which is also compared one by one through the loop, and the maximum value is assigned to max, and the last remaining number is the middle number
function mySort() {
    var min = arguments[0];
    var max = arguments[0];
     var mid = arguments[0];
     for (var i = 0; i < arguments.length; i++) {
          if (arguments[i] < min) {
               min = arguments[i]
           }else if(arguments[i] > max){
               max = arguments[i];
           }else if(arguments[i] != min && arguments[i] != max){
              mid = arguments[i];
           }
     }
       console.log(min,mid,max);
}
 mySort(1,4,2);

2. Method 2 (bubble sort)

  • Later I will use an article to talk about bubble sorting and selection sorting
var numSort = function () {
    var arr = arguments;
    for (var j = 0; j < arr.length - 1; j++) {
        for (var i = 0; i < arr.length - 1 - j; i++) {
            if (arr[i] > arr[i + 1]) {
                var temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp
            }
        }
    }
    return arr
}
console.log(numSort(1, 6, 4, 5, 123, 213, 3, 8, 9, 4, 5));

3. Write a function to find whether a number is a prime number

  • Idea: A prime number can only be reduced by itself and 1. First, define a flag with an initial value of true. Using for, the initial value is 2, because 1 is not needed, and then divide the number by 2 to the number of itself -1 , if there is one, the flag becomes false, indicating that the number is not a prime number.
var prime = function (num) {
    var flag = true
    for (i = 2; i < num; i++) {
        if (num % i == 0) {
            flag = false
        }
    }
    if (flag == false) {
        console.log("这个数不是质数");
    }
}
prime(9)

4. Write a function to calculate the number of odd numbers that can be formed between any two numbers. The number must be a single digit

  • For example: Calculate the odd numbers that can be formed between 0-3 are 01, 03, 11, 13, 21, 23, 31, 33
var o=function(num1,num2){
        for(var i=0;i<=num1;i++){
            let num;
            for(var j=0;j<=num2;j++){
                num=i*10+j;
                if(j%2!=0){
                    console.log(num);
                }
            }
        }
}
o(1,3)

5. A company uses public telephones to transmit data. The data is a four-digit integer, which is encrypted during the transmission process. The encryption rules are as follows: add 5 to each number, and then replace the number with the remainder divided by 10, and then Exchange the first digit with the fourth digit, and the second digit with the third digit, please write a function, pass in the original text, and output the ciphertext

function pa(a) {
    var a1 = parseInt(a / 1000); //千
    var a2 = parseInt(a / 100) % 10; //百
    var a3 = parseInt(a / 10) % 10; //十
    var a4 = a % 10; //十

    a1 = (a1 + 5) % 10
    a2 = (a2 + 5) % 10
    a3 = (a3 + 5) % 10
    a4 = (a4 + 5) % 10

    var a5
    a5 = a4
    a4 = a1
    a1 = a5

    a5 = a2
    a2 = a3
    a3 = a5

    var num;
    num = a1 * 1000 + a2 * 100 + a3 * 10 + a4
    console.log(num);
}
pa(1234)

Guess you like

Origin blog.csdn.net/liu0218/article/details/126532328