How do I use parseFloat in this algorithm?

0

I have created an algorithm that receives the 4-month bill, which calculates the mean and shows the approval result. For the approval result I did as follows.

Approval Result:

Approval with maximum grade = 10. Approval above average = 8 to 9. Approved on average = 7. Recovery = 6. Failure = 1 to 5.

Problem = If the user enters the value 7 in each note the average will be 7 and will show "Approve on average = 7" However, if you enter the value of 7.3 in each note it will show the following message "Disapproved" in case was to show "Above Average Approval".

I have to convert something to ParseFloat, but I'm not sure where to insert it.

var s1 = prompt ( " Insira a nota do 1 Bimestre : ") ; 
var s2 = prompt ( " Insira a nota do 2 Bimestre : " ) ; 
var s3 = prompt ( " Insira a nota do 3 Bimestre : " ) ; 
var s4 = prompt ( " Insira a nota do 4 Bimestre : " ) ;


var s1 = Number (s1) ; 
var s2 = Number (s2) ; 
var s3 = Number (s3) ; 
var s4 = Number (s4) ; 

var media = parseFloat (( s1 + s2 + s3 + s4 ) / 4 ); 

if (media ==10)
{
	alert ( "Aprovação com nota maxima! " ) ; 
}  

else if ((media == 8) || ( media == 9 ) )
{
	alert ( " Aprovação acima da  média ! " ) ; 
} 

else if (media == 7) 
{
	alert ( " Aprovação com nota na média! " ) ; 
}

else if ( media == 6 ) 
{
	alert ( "Recuperação ! " ) ; 
}

else 
{
	alert ( " Reprovado ! " ) ; 
}
    
asked by anonymous 16.08.2018 / 17:50

2 answers

0

It should be this way to work, parseFloat goes directly to the variable:

var s1 = prompt("Insira a nota do 1 Bimestre: "); 
var s2 = prompt("Insira a nota do 2 Bimestre: "); 
var s3 = prompt("Insira a nota do 3 Bimestre: "); 
var s4 = prompt("Insira a nota do 4 Bimestre: ");

var s1 = parseFloat(s1); 
var s2 = parseFloat(s2); 
var s3 = parseFloat(s3); 
var s4 = parseFloat(s4); 

var media = (( s1 + s2 + s3 + s4 ) / 4 );

if(media == 10){

  	alert("Aprovação com nota maxima!");

}else if (media > 7) {

  	alert("Aprovação acima da média !");

} else if(media == 7) {

  	alert(" Aprovação com nota na média!");

} else if( media == 6 ) {

  	alert("Recuperação!");

} else{

  	alert("Reprovado!");

}
    
16.08.2018 / 18:25
0

The problem is that in your ifs you are not covering the values between notes (such as 7.3). By modifying the checks for the following code your logic should meet that expected:

if (media ==10) {
  alert ( "Aprovação com nota maxima! " ) ;
} else if ( media > 7) {
  alert ( " Aprovação acima da  média ! " ) ;
} else if (media == 7) {
  alert ( " Aprovação com nota na média! " ) ;
} else if ( media == 6 ) {
  alert ( "Recuperação ! " ) ;
} else {
  alert ( " Reprovado ! " ) ;
}
    
16.08.2018 / 17:57