JavaScript Initial Understanding - Notes on Statistical Examination Subject Achievements

Keywords: Javascript

Statistics of test subjects

  1. Using prompt () method to input the number of exam subjects requires non-zero and non-negative numbers, otherwise give the corresponding prompt and exit the program.
  2. According to the number of subjects, the prompt() method is used to input and accumulate the results of each subject. The results must be non-negative. Otherwise, the corresponding prompt is given and the procedure is withdrawn.
  3. If the input is correct, the total score of the subject will be output. Some pictures are shown as follows:

  1. <input name="btn" type="button" value="Statistical examination results" οnclick="count(prompt('Please enter the number of subjects in the exam.'));" />
    <script type="text/javascript">
        function count(num){//There is a parameter function, num=prompt('Please enter the number of subjects')
            var sum=0;
            if(isNaN(num)){//If num is not a number, then output alert, otherwise perform the following conditional judgment
                alert("The input is not a number!");
            }else if(num<=0){
                alert("Input test subjects are invalid");
            }else{
                for(var i=1;i<=num;i++){//Final i=num
                    var score=prompt("Please lose the first place."+i+"Achievements in subjects:");
                    if(isNaN(score)){
                        alert("Achievements must be numbers!");
                        break;//Exit the for loop directly
                    }else if(score<0)
                    {
                        alert("Achievements cannot be negative!");
                        break;
                    }else
                        sum+=parseFloat(score);//Equal to sum=sum+paraseFlow(sorce)
    					//Because the prompt() method returns a string of data, the parseFlow() function is used to convert the string to floating-point numbers.
    					//sum+=parseInt(score) is not available here because he can't calculate floating point numbers accurately.
                }
            }
            if(sum!=0)//If the previous break and the variable sum=0 were declared earlier, the following statement would not be executed
                alert(num+"The total results of the subjects are as follows:"+sum);
        }
    
    </script>

Of course, we can also modify it by changing the parametric function to the parametric function.

  1. <input name="btn" type="button" value="Statistical examination results" οnclick="count();" />
    <script type="text/javascript">
    
        function count(){//Parameter free function
    	var num = prompt('Please enter the number of subjects in the exam.')
            var sum=0;
            if(isNaN(num)){Conditional Judgment
                alert("The input is not a number!");
            }else if(num<=0){
                alert("Input test subjects are invalid");
            }else{
                for(var i=1;i<=num;i++){
                    var score=prompt("Please lose the first place."+i+"Achievements in subjects:");
                    if(isNaN(score)){
                        alert("Achievements must be numbers!");
                        break;
                    }else if(score<0)
                    {
                        alert("Achievements cannot be negative!");
                        break;
                    }else
                        sum+=parseFloat(score);
    					
                }
            }
            if(sum!=0)
                alert(num+"The total results of the subjects are as follows:"+sum);
        }
    	
    
    
    </script>

For button tags, we can also write as follows: <button_nclick= "count ()"> Statistical Examination Results </button>.

Finally, we can expand further (if the input error gives a hint and re-input):

<button οnclick="count()">Statistical examination results</button>
<script type="text/javascript">

    function count(){//Parameter free function
		var num = prompt('Please enter the number of subjects in the exam.')
        var sum=0;
        while(isNaN(num)){
            alert("The input is not a number!");
			num = prompt('Re-enter the number of exam subjects')
        }
		while(num<=0){
            alert("Input test subjects are invalid!");
			num = prompt('Re-enter the number of exam subjects')
        }
		for(var i=1;i<=num;i++){
                var score=prompt("Please lose the first place."+i+"Achievements in subjects:");
				while(isNaN(score)){
					alert("Achievements must be numbers!");
					score=prompt("Please lose the first place."+i+"Achievements in subjects:");
					}
                while(score<0){
					alert("Achievements cannot be negative!");
					score=prompt("Please lose the first place."+i+"Achievements in subjects:");
                }
                 sum+=parseFloat(score);
					
            }
        if(sum!=0)
            alert(num+"The total results of the subjects are as follows:"+sum);
       
    }
	


</script>

Then the above code realizes that the input error can be re-entered, so can we modify the final code and get the total score of the last displayed code by who plus whom?

(To be completed):

Posted by Xanthis on Mon, 30 Sep 2019 04:23:06 -0700