JavaScript exercise #Day02

JavaScript Practice Questions # Day02 Assignment

Assignment 01
Mark and John PK their BMI (Body Mass Index). Body mass index is calculated using this formula:

BMI = mass / height^2 = mass / (height * height)

1
Among them, mass is the weight in kg; height is the height in meters.
Please use JavaScript to write a program named bmi.js to complete the following tasks:
1. Use variables to store the weight and height of Mark and John;
2. Calculate the BMI of the two;
3. Create a Boolean variable to contain Mark’s Is the BMI higher than John;
4. Print a string to the console, including the variables of the third step (for example, "Is Mark's BMI higher than John? true"

// BMI = mass / height^2 = mass / (height * height)
mass = [58,43]    //存储体重
height = [175,165]//存储身高
function BMI(mass,height){
    
       //定义BMI计算方法
    bmi = mass / (height * height)
    return bmi
}
mark_bmi = BMI(mass[0],height[0]);
jonu_bmi = BMI(mass[1],height[1]);
if (mark_bmi > jonu_bmi){
    
    
    var b = true
}else var b = false
console.log("Mark的BMI为:"+mark_bmi)
console.log("John的BMI为:"+jonu_bmi)
console.log("Mark的BMI是否比John更高?"+b)

The result of the operation is shown in the figure.
Insert picture description here
Job 02
John and Mike are both playing basketball on different teams. In the last three games, John's team scored 89, 120, and 103 points, while Mike's team scored 116, 94, and 123 points. Write the program in JavaScript and save it as ball.js to complete the following tasks:
1. Calculate the average score of each team;
2. Determine which team wins on average (the highest average score), and print the winner to the console. The average score is also included in the output;
3. Then change the score to show different winners. Don't forget to consider that there may be a tie (the average points are the same); Mary also plays basketball and her team scored 97, 134 and 105 points.
4. As before, record the average score winner to the console; as before, change the score to produce a different winner, remember that there may be a tie.

John_avg = (89+120+103)/3
Mike_avg = (116+94+123)/3
console.log("John队的平均分是"+John_avg)
console.log("Mike队的平均分是"+Mike_avg)
//2.4 决定哪支球队平均获胜(平均分最高),并将胜者打印到控制台。在输出中也包括平均分;
if(John_avg > Mike_avg){
    
        
    console.log("John是获胜队伍!" + "John的平均分是:"+John_avg);
}
else if (Mike_avg>John_avg){
    
       
    console.log("Mike是获胜队伍!" + "Mike的平均分是:"+Mike_avg);
}
else{
    
       
    console.log("平局!");
}
//1. 计算每队的平均分;
John_avg = (89 + 120 +103) / 3;
Mike_avg = (116 + 94 +123) / 3;
Mary_avg = (97 + 134 +105) / 3;
console.log("\nJohn的平均分是:"+John_avg+"\n" +"Mike的平均分是:"+Mike_avg+"\n"+"Mary的平均分是:"+Mary_avg)
//2.4 决定哪支球队平均获胜(平均分最高),并将胜者打印到控制台。在输出中也包括平均分;
if((John_avg > Mike_avg) && (John_avg > Mary_avg)){
    
        
console.log("John是获胜队伍!" + "John的平均分是:"+John_avg);
}
else if ((Mike_avg>John_avg) && (Mike_avg>Mary_avg)){
    
       
 console.log("Mike是获胜队伍!" + "Mike的平均分是:"+Mike_avg);
 }
 else if((Mary_avg>John_avg) && (Mary_avg>Mike_avg)){
    
      
   console.log("Mary是获胜队伍!" + "Mary的平均分是:"+Mary_avg);
   }
   else{
    
       
    console.log("平局!");
    }

    //3、5 然后改变分数来显示不同的赢家。
let John_avg1 = (123 + 120 +93) / 3;
let Mike_avg1 = (126 + 93 +120) / 3;
let Mary_avg1 = (100 + 134 +105) / 3;
console.log("\nJohn的平均分是:"+John_avg1+"\n" +"Mike的平均分是:"+Mike_avg1+"\n"+"Mary的平均分是:"+Mary_avg1)
if((John_avg1 > Mike_avg1) && (John_avg1 > Mary_avg1)){
    
        
console.log("John是获胜队伍!" + "John的平均分是:"+JohnAvg1);
}
else if ((Mike_avg1>John_avg1) && (Mike_avg1>Mary_avg1)){
    
        
console.log("Mike是获胜队伍!" + "Mike的平均分是:"+Mike_avg1);
}
else if((Mary_avg1>John_avg1) && (Mary_avg1>Mike_avg1)){
    
        
console.log("Mary是获胜队伍!" + "Mary的平均分是:"+Mary_avg1);
}
else{
    
       
 console.log("平局!");
 }

Screenshot of code running
Insert picture description here
Job 03
uses JavaScript to write the program, named star.js, and outputs the following graphics on the console:
Insert picture description here

for(i=1;i<9;i=i+2){
    
    
    a=" ".repeat((7-i)/2);
    j=i
    if(j>1){
    
    j=j-(j/2-1)}
    b="* ".repeat(j);
    console.log(a+b)
}
for(i=5;i>0;i=i-2){
    
    
    a=" ".repeat((7-i)/2);
    j=i
    if(j>1){
    
    j=j-(j/2-1)}
    b="* ".repeat(j);
    console.log(a+b)
}

Code running screenshot
Insert picture description here

Guess you like

Origin blog.csdn.net/With__Hope/article/details/109009544