# Judgment exercises

## Judgment exercises

1. Enter an integer to judge whether it is greater than 0 and less than 0 or equal to 0

```	// Enter an integer
var num = 0;
// judge
if (num > 0) {
// output
console.log(num + "The value of is greater than 0");
} else if (num < 0) {
// output
console.log(num + "The value of is less than 0");
} else {
// output
console.log(num + "The value of is equal to 0");
}
```

2. Input a number to judge whether it is odd or even, and output the result

```	// Enter a number
var num1 = 8;
// judge
if (num1 % 2 == 1) { // Odd number
// Output result
console.log(num1 + "It's an odd number");
} else if (num1 % 2 == 0) { //Even numbers
// Output result
console.log(num1 + "It's an even number");
} else {
// Output result
console.log("Incorrect input");
}
```

3. Enter the month to display the number of days in the month (leap year is not considered)

```	// Input month
var month = 4;
var day = 0;
// switch (judged variable){
// 	case 'value of variable':;
// }
// Strict equivalence = = =
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
// Display the number of day s in the current month
console.log(month + "Month has" + day + "day");
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
// Display the number of day s in the current month
console.log(month + "Month has" + day + "day");
break;
case 2:
// Assign value to day
day = 28;
// Display the number of day s in the current month
console.log(month + "Month has" + day + "day");
break;
default:
console.log('Wrong month entered');
}
```

4. Develop a software, according to the formula (height - 108) 2 = weight, it can float about 10 jin. To observe whether the weight of the tester is appropriate prompt: input height (CM) to calculate weight (Jin), and then judge that the difference between the calculated weight and the input weight is within plus or minus 10 jin, you can use Math.abs() to take the absolute value

```	// Enter the actual height and weight
var height = 180;
var weight = 140;
// Calculate the standard weight according to the actual height
var result = (height - 108) * 2;
// It is normal to compare the difference between actual weight and standard weight within 10
if (Math.abs(weight - result) < 10) { // -10-10 0-10
// Output standard or not
console.log('You are perfect.');
} else {
// Output standard or not
console.log('Rubbing');
}
```

5. It is known that the monthly interest rates of the bank's lump sum deposits with different maturities are as follows:
Monthly interest rate =
0.63% term = 1 year
0.66% term = 2 years
0.69% term = 3 years
Input the principal and age of the deposit, and find the total of interest and principal that can be obtained from the bank at maturity. (the calculation formula of interest is: interest = principal × monthly interest rate × 12 × deposit period. (either switch or else - if)

```	// Enter principal and age
var money = 1000000; // Principal
var year = 5; // Years
var interest = 0; // Interest
var sum = 0; // Principal sum
// Determine the current age,
if (year == 1) {
// Interest = principal × monthly interest rate × 12 × deposit period
interest = money * (0.63 / 100) * 12 * year;
// Output principal and
sum = money + interest;
// I can get sum from the bank in year
console.log("I deposit" + year + "Can get it from the bank in" + sum + "element");
} else if (year == 2) {
// Interest = principal × monthly interest rate × 12 × deposit period
interest = money * (0.66 / 100) * 12 * year;
// Output principal and
sum = money + interest;
// I can get sum from the bank in year
console.log("I deposit" + year + "Can get it from the bank in" + sum + "element");
} else if (year == 3) {
// Interest = principal × monthly interest rate × 12 × deposit period
interest = money * (0.69 / 100) * 12 * year;
// Output principal and
sum = money + interest;
// I can get sum from the bank in year
console.log("I deposit" + year + "Can get it from the bank in" + sum + "element");
} else {
console.log('Sorry, there is no such business');
}
```

6. There is a function:
// y = x x < 1
// y = 2x 1<= x < 10
// y = 3x - 11 x >= 10
/ / input x

```	var x = 2;
var y;
// Determine which range x belongs to
if (x < 1) {
// Output y
y = x;
// What is the value of y when x is the output result = = >
console.log("x by" + x + "When,y The value is" + y);
} else if (x >= 1 && x < 10) {
// Output y
y = 2 * x;
// What is the value of y when x is
console.log("x by" + x + "When,y The value is" + y);
} else {
// Output y
y = 3 * x - 11;
// What is the value of y when x is
console.log("x by" + x + "When,y The value is" + y);
}
```

7. For a positive integer with no more than 5 digits, the following requirements are required: 1. Find out how many digits it is; 2. Print out the digits in reverse order

```	var num = parseInt(Math.random() * 100000);// Between 0-10000
if (num < 10) {//One digit
console.log(num + "It's one digit.,Output this number in reverse order" + num);
} else if (num > 10 && num < 100) {//Two digit number
// For example, 76
var g = num % 10;//Bit
var s = parseInt(num / 10);//Ten place
console.log(num + "It's two digits.,Output this number in reverse order" + g + ' ' + s);
} else if (num > 100 && num < 1000) {//Three digit number
// For example, 324
var g = num % 10;// Bit
var s = (parseInt(num / 10)) % 10;//Ten place
var b = parseInt(num / 100);//Hundred bit
console.log(num + "It's three digits.,Output this number in reverse order" + g + ' ' + s + ' ' + b);
} else if (num > 1000 && num < 10000) { // Four digit number
// For example, 1234
var g = num % 10;// Bit
var s = (parseInt(num / 10)) % 10;//Ten place
var b = (parseInt(num / 100)) % 10;//Hundred bit
var q = parseInt(num / 1000);//Thousand bit
console.log(num + "It's four digits.,Output this number in reverse order" + g + ' ' + s + ' ' + b + ' ' + q);
} else if (num > 10000 && num < 100000) { //Five place
// For example, 54321
var g = num % 10;// Bit
var s = (parseInt(num / 10)) % 10;//Ten place
var b = (parseInt(num / 100)) % 10;//Hundred bit
var q = (parseInt(num / 1000)) % 10;//Thousand bit
var w = parseInt(num / 10000);//Thousand bit
console.log(num + "It's five digits.,Output this number in reverse order" + g + ' ' + s + ' ' + b + ' ' + q + ' ' + w);
}
```

8. According to a digital date, judge the date as the day of the year (a little difficult)
For example: 20160211, the calculated result is 42 31 in January + 11 in February = 42
Note: only in leap year, February is different

```	var year = 2016;// year
var month = 5;// month
var day = 11;// day
// First, judge the leap year of the year and get the different values of February
var flag = true; // Give a flag true to indicate leap year
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { // Leap year
flag = true;// Is leap year value unchanged
} else {
flag = false; // false if not leap year assignment
}
var days = 0;//Define day variables
// Judge days by month
// Note that swicth penetration (not the teacher's crossing) here means that every case is executed without break (you can try to see the execution order with break points)
// For example, the value of December needs to be added with the value of the first 11 months and the value of the current day entered. If you don't write break, it will run from 11 to the end
// So month minus 1 + current day is the value to be used
// The value entered is 2016 5 11
switch (month - 1) {
case 11:
days = days + 30;// 0+30
// Do not write break will continue to execute downward
case 10:
// Days is a process of accumulation. At this time, days becomes 0 + 30
days = days + 31; // 0+30+31
case 9:
days = days + 30; // 0+30+31+30
case 8:
days = days + 31; // 0+30+31+30+31
case 7:
days = days + 31; // 0+30+31+30+31+31
case 6:
days = days + 30; // 0+30+31+30+31+31+30
case 5:
days = days + 31; // 0+30+31+30+31+31+30+31
case 4:
days = days + 30; // 0+30+31+30+31+31+30+31+30
case 3:
days = days + 31; // 0+30+31+30+31+31+30+31+30+31
case 2: // You need to use the flag above to determine how many days to add
if (flag == true) {// 28 days in leap year
days = days + 28; // 0+30+31+30+31+31+30+31+30+31+28
} else {
days = days + 29; // 0+30+31+30+31+31+30+31+30+31+27
}
case 1:
days = days + 31;
case 0: // After the first 11 months, you only need to add the day after the current month
days = days + day;
default: ;
}
console.log(year + "year" + month + "month" + day + "Day is" + year + "The first" + days + "day");
```

9. Programming to realize the following functions: read in two operands (data1 and data2) and an operator (op),
Evaluates the value of the expression data1 op data2, where OP can be +, -, *, /. ]
Enter three variables data1 data2 op

```	var data1 = 10; // First value
var data2 = 10; // Second values
var op = "+"; // operator
var sum = 0;
// Determine the value of op
switch (op) {
case "+":
sum = data1 + data2;
// Sum of data1 op data2 is sum
console.log(data1 + op + data2 + "The sum is" + sum);
break;
case "-":
sum = data1 - data2;
// The difference of data1 op data2 is sum
console.log(data1 + op + data2 + "The difference is" + sum);
break;
case "*":
sum = data1 * data2;
// The product of data1 op data2 is sum
console.log(data1 + op + data2 + "Product is" + sum);
break;
case "/":
if (data2 != 0) {
sum = data1 / data2;
// The product of data1 op data2 is sum
console.log(data1 + op + data2 + "The business is" + sum);
} else {
console.log('Pro, divisor can't be 0, you didn't graduate from primary school!!!');
}
break;
default:
console.log('I can't work out what you input');
}
```

10. Enter a four digit number to find the sum of the squares of each digit.

```	// For example: input 1234, output 30. Math.pow(2,2) is the second power of 2
var num = parseInt(Math.random() * 10000);// Random four digit number
// Find out every bit, add up every square
var g = num % 10;// Bit
var s = (parseInt(num / 10)) % 10;//Ten place
var b = (parseInt(num / 100)) % 10;//Hundred bit
var q = parseInt(num / 1000);//Thousand bit
var sum = Math.pow(g, 2) + Math.pow(s, 2) + Math.pow(b, 2) + Math.pow(q, 2)
console.log("The sum of the squares of your numbers is" + sum);
```

11. Regulations on checked baggage by Railway: if the weight of baggage does not exceed 50 kg, the check-in fee shall be charged at 0.15 yuan per kg; if the weight exceeds 50 kg, the excess part shall be charged at 0.10 yuan per kg. Compile a program to complete the automatic billing work.

```	var weight = 100;
var money = 1;
if (weight <= 50) {
money = weight * 0.15;
} else {
money = 50 * 0.15 + (weight - 50) * 0.1;
}
console.log("Baggage weight" + weight + "Consignment fee is" + money)
```  Published 8 original articles, won praise 1, visited 34

Tags: less Programming

Posted on Sun, 15 Mar 2020 01:39:41 -0700 by adredz