1

I make a condition with Else-If to print a line like [You got A+, You got D]. Now I want to store one valid output line in a variable outside the condition.

var gradeMark = Math.round(totalNumber / 6);

//Generate grade mark
if (gradeMark >= 80) {
    console.log("You got A+");
} else if (gradeMark >= 70) {
    console.log("You got A");
}....
.....
else if (gradeMark >= 0) {
    console.log("You got F");
} else {
    console.log("It's not valid mark");
}

//Getting grading mark
var validGradeMark = 
3
  • Whats the problem? Set your validGradeMark before if statement fill it inside,, and use later... Commented Apr 4, 2021 at 2:32
  • I need //It's not valid mark or valid output which will be printed after true condition as a variable. Commented Apr 4, 2021 at 2:34
  • 1
    I think you missing some basics: jsfiddle.net/ikiK_Cro/wjotkbs2/2 Commented Apr 4, 2021 at 2:38

1 Answer 1

1

You can use defining variable instead of console log and it is out of condition now.

let gradeMark = Math.round(totalNumber / 6);
let validGradeMark;

// Generate grade mark
if (gradeMark >= 80) {
   validGradeMark = "You got A+";
} else if (gradeMark >= 70) {
   validGradeMark = "You got A";
}....
.....
else if (gradeMark >= 0) {
   validGradeMark = "You got F";
} else {
   validGradeMark = "It's not valid mark";
}

// Getting grading mark
console.log(validGradeMark);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.