How do I use parseFloat in this algorithm?

Asked

Viewed 86 times

0

I created an algorithm that gets a 4-quarter note , which makes an average calculation and shows the result of the approval. For the result of the approval I did as follows .

Result of Approval :

Pass with maximum grade = 10 . Above average approval = 8 to 9 . Approval 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 " Pass on average = 7 " However, if you insert the value of 7.3 in each note will show the following message " Failed " in case it was to show " Pass above average ".

Obs ( I know I have to transform something for 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 ! " ) ; 
}

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

2 answers

0

The problem is that in your ifs you are not covering the values between the notes (such as 7.3). Modifying the checks to the following code your logic should meet the 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 ! " ) ;
}
  • I have already found the error . Instead of " if (media == 7) " you should put " Else if ((media > 7) && ( media < 10 ) ". Therefore, any average that is greater than 7 and less than 10 will display the correct message .

0

It shall remain so for the operation, the parseFloat goes directly into 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!");

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.