[GIT BASH IMAGE][1]
https://i.stack.imgur.com/Akyjx.png
Below I've tried adding in some number 1-100 inside the function itself for example;
Function grade(85)
I've also tried assigning a variable let = 85.
I guess I'm not understanding what they mean by "Receive a score out of 100".
I need help getting this number plugged into this script here. I have included the gitbash error that I'm receiving which makes me think that it's something that needs to be included with the function.
Thank you
//Grade Calculator
/*
Using the grade function below do the following:
1. Receive a score out of 100
2. Return the corresponding letter grade following this grade scale:
90-100 = A
80-89 = B
70-79 = C
60-69 = D
below 60 = F
*/
function grade(){
if(grade = 100 && grade >=90 )
{
return 'you got an A';
}
if(grade >= 80 && grade <= 89 )
{
return 'you got a B';
}
if(grade >= 70 && grade <= 79 )
{
return 'you got a C';
}
if(grade >= 60 && grade <= 69 )
{
return 'You got a D';
}
if(grade <= 60 )
{
return 'you got a F';
}
}
[1]: https://i.stack.imgur.com/Akyjx.png
You don't need the condition for max
function grade(score){
if(score >=90 )
{
return 'you got an A';
}
else if(score >= 80)
{
return 'you got a B';
}
else if(score >= 70)
{
return 'you got a C';
}
else if(score >= 60)
{
return 'You got a D';
}
else
{
return 'you got a F';
}
}
console.log(grade(78));
I believe what you have done is almost correct. They want you to pass the score out of 100 and tell the grade. So after some major tweaks in your code it looks like this.
function grade(score){
if(score == 100 || score >=90 )
{
return 'you got an A';
}
else if(score >= 80 && score <= 89 )
{
return 'you got a B';
}
else if(grade >= 70 && grade <= 79 )
{
return 'you got a C';
}
else if(score >= 60 && score <= 69 )
{
return 'You got a D';
}
else
{
return 'you got a F';
}
}
console.log(grade(85));
Change the score in the grade calling function at the bottom [grade(85);] and check for change in the grade.
Hope this solves your question.
You did not defined the function argument. provide an argument to the function . you refereed the function name in the function body that is the problem with your code
function grade(mark){
if(mark = 100 && mark >=90 )
{
return 'you got an A';
}
if(mark >= 80 && mark <= 89 )
{
return 'you got a B';
}
if(mark >= 70 && mark <= 79 )
{
return 'you got a C';
}
if(mark >= 60 && mark <= 69 )
{
return 'You got a D';
}
if(mark <= 60 )
{
return 'you got a F';
}
}
Related
Just playing around with functions..
The live server shows undefined.
However; when I just run the javascript code-the correct greetings show.
Why is this?
What can I do to fix it?
const today = new Date();
let now = today.getHours(); //0-23
let time;
/*morning: 0300 - 1100
*morning >= 0300 && morning < 11
*afternoon 1100-1900
*afternoon >= 11 && afternoon < 19
*evening 1900 - 0300
*evening >= 19 && evening < 3
*/
if (now >= 5 && now < 11) {
time = 'morning';
} else if (now >= 11 && now < 17) {
time = 'afternoon';
} else if (now >= 17 && now < 23) {
time = 'night';
} else {
time = 'sleep';
}
//console.log(time);
function greeting() {
if (time === 'morning') {
console.log('Good Morning!');
console.log('I love you!');
console.log("You're doing GREAT!");
console.log('Nice butt!');
} else if (time ==='afternoon') {
console.log('Good afternoon!');
console.log("You're a genius!");
console.log('Just keep swimming!');
console.log('Progress not perfection!');
} else if (time === 'night') {
console.log('Goodnight, Princess!');
console.log('Time for a nightcap.');
console.log ('Sleep tight!');
console.log ('Get NAKED!');
} else {
console.log('GO TF\' TO SLEEP');
console.log('GO TF\' TO SLEEP');
console.log('GO TF\' TO SLEEP');
console.log('GO TF\' TO SLEEP');
}
}
//greeting();
document.write('<h1>' + greeting() + '</h1>');
??
console.log() prints to the console (F12 in normal browsers). You probably want to replace all console.log() with document.write('<yourTag>Message</yourTag>');.
Otherwise, if you want to use the output of the function the you can create an empty string variable and concat your messages and return them.
function functionName() {
let result = '';
result = result + 'my Test';
result = result + ' my Test 2';
return result;
}
Of course you can use structures control structures (if/else) with both ways.
greeting() method does not return anything that's why you are getting undefined
function greeting(){
return 'Say Hello!'
}
Now this will shows you
<h1>Say Hello!</h1>
// The challenge is to say if someone got 15 out of 20 questions, they would have 75% score, which is a C.
// 15/20 is 75%...you got a C (75%)!
// 90 to 100, A, B 80-89, C 70-79, D 60-69, F 0-59
How do I add the letter grades into my function string return function call?
let studentGrade = function (score, total =100) {
let totalGrade = (score / total)
let totalPercent = (totalGrade * 100)
if (score >=90 && score <=100) {
console.log('You got an A!')
}
else if (score >=80 && score <=89) {
console.log('You got an B!')
}
else if (score >=70 && score <=79) {
console.log('You got an C!')
}
else if (score >=60 && score <=69) {
console.log('You got a D!')
}
else if (score <=59 ) {
console.log('You got an E!')
}
return (`You scored ${score} of a total of ${total} questions, that is ${totalPercent}%, which is a X`)
}
let studentOne = studentGrade (50, 100)
console.log(studentOne)
Add an unset variable at the beginning like let thisGrade;. Set thisGrade to A or whatever the grade is, in the if-else logic.
Then you can use template substitution to include ${thisGrade} in your return value.
You can also reduce repetition by having only one console.log statement after the termination of the if-else logic, which is also referring to thisGrade.
Assuming that you're only interested in the letter being returned you could structure your code something like this.
let studentGrade = (score, total = 100) => {
const totalGrade = (score / total);
const totalPercent = (totalGrade * 100);
let grade;
if (score >=90 && score <=100) {
grade = "A";
} else if (score >=80 && score <=89) {
grade = "B";
} else if (...) {
...
} else {
...
}
console.log(`You got a ${grade}!`); // Could be grammatically incorrect but you could wrap logic to make this correct
return grade;
}
let studentOne = studentGrade(95, 100);
console.log(studentOne); // "A"
By doing this you have the option to remove the log statement from the studentGrade function entirely giving it the single responsibility of calculating the grade and not having to deal with the output.
Thank you, that helped!
let studentGrade = function (score, total) {
let totalPercent = (score / total) * 100
let letterGrade = ''
if (totalPercent >= 90) {
letterGrade = 'A'
}
else if (totalPercent >=80) {
letterGrade = 'B'
}
else if (totalPercent >=70) {
letterGrade = 'C'
}
else if (totalPercent >=60) {
letterGrade = 'D'
}
else {
letterGrade = 'F'
}
return `You scored ${score} of a total of ${total} questions, that is
${totalPercent}%, which is a ${letterGrade}`
}
let studentOne = studentGrade (50, 100)
console.log(studentOne)
I just have no idea what I did wrong to get undefined answers logged to my console.
I've tried deleting the && clauses but this didn't seem to help at all; actually it made it worse.
function gradeCalculator(grade) {
if (grade >= 90) {
console.log ("A");
} else if (grade >= 80 && grade <= 89) {
console.log ("B");
} else if (grade >= 70 && grade <= 79) {
console.log ("C");
} else if (grade >= 60 && grade <= 69) {
console.log ("D");
} else (grade <= 59)
console.log ("F");
}
console.log(gradeCalculator(92)); //=> "A"
console.log(gradeCalculator(84)); //=> "B"
console.log(gradeCalculator(70)); //=> "C"
console.log(gradeCalculator(61)); //=> "D"
console.log(gradeCalculator(43)); //=> "F"
I expected the output to log to the console as:
'A'
'B'
'C'
'D'
'F'
But it actually came out as:
'A'
'F'
undefined
'B'
'F'
undefined
'C'
'F'
undefined
'D'
'F'
undefined
'F'
undefined
I would really appreciate any and all help, thank you so much! =)
function gradeCalculator(grade) {
if (grade >= 90) {
console.log("A");
} else if (grade >= 80 && grade <= 89) {
console.log("B");
} else if (grade >= 70 && grade <= 79) {
console.log("C");
} else if (grade >= 60 && grade <= 69) {
console.log("D");
} else {
console.log("F");
}
}
gradeCalculator(92); //=> "A"
gradeCalculator(84); //=> "B"
gradeCalculator(70); //=> "C"
gradeCalculator(61); //=> "D"
gradeCalculator(43); //=> "F"
The problems were:
else (grade <= 59) - else doesn't take a condition, and this case wasn't wrapped in { and }
console.log(gradeCalculator(92)); //=> "A" - The function wasn't returning anything, so you were console.log'ing an undefined value (since the logging was in the function itself)
function gradeCalculator(grade) {
if (grade >= 90) {
console.log ("A");
} else if (grade >= 80 && grade <= 89) {
console.log ("B");
} else if (grade >= 70 && grade <= 79) {
console.log ("C");
} else if (grade >= 60 && grade <= 69) {
console.log ("D");
} else if(grade <= 59){
console.log ("F");
}
}
Your last else should be else if with a condition.
(You can just put else but else if with a condition is more maintainable.
I'm new to code so my mistake is probably obvious but,
When I go and test the calculator, it reads "A" then where ever the proper grade should be then Undefined. Idk where I'm going wrong.
I've tried a lot of different variations, and can't get the calculator to work properly. I'm not sure where the problem is.
function calculateGrade(grade) {if (parseInt >= "90"){
alert ("A");
}
else if (parseInt >= "80" === parseInt < "90"){
alert ("B");
}
else (parseInt >= "70" === parseInt < "80");{
alert ("C");
}
if (parseInt >= "60" === parseInt < "70"){
alert ("D");
}
else if (parseInt < "60"){
alert ("F");
}}
var inputGrade = prompt("Enter a grade:");
var parsedInt = parseInt(inputGrade);
var finalGrade = calculateGrade(parsedInt);
alert(finalGrade);
Suggestions and errors in your code
You already have parsed into number, so leave the '90' quotes from the numbers.
Don't use magic names and global variables. Your function accepts the grade argument. Use it in your code.
Your if else conditions are wrong. Some of them are without else if. You can left the second part of each else if conditions.
Return the final which you expect from the function.
function calculateGrade(grade) {
let final = '';
if (grade >= 90) {
final = 'A';
} else if (grade >= 80) {
final = 'B';
} else if (grade >= 70) {
final = 'C';
} else if (grade >= 60) {
final = 'D';
} else {
final = 'F';
}
return final;
}
let inputGrade = prompt("Enter a grade:");
let parsedInt = Number.parseInt(inputGrade);
let finalGrade = calculateGrade(parsedInt);
alert(finalGrade);
You could use a patttern with early exit and start frim the smallest value check to the greatest value.
The early exit returns a function and no more check is performed, because the function has ended with return statement.
For taking an integer value with parseInt, you should use a radix of 10, because without and with leading zero the number is treated as octal number, which you do not want.
A last advice, please do not use name of build in functions or objects or reserved words as variable names. This could lead to not proper working code and to searching without reason.
function calculateGrade(grade) {
if (grade < 50){
return "F";
}
if (grade < 60){
return "E";
}
if (grade < 70) {
return "D";
}
if (grade < 80) {
return "C";
}
if (grade < 90) {
return "B";
}
return "A";
}
var inputGrade = prompt("Enter a grade:"),
grade = parseInt(inputGrade, 10),
finalGrade = calculateGrade(grade);
console.log(finalGrade);
Try it
function calculateGrade(grade) {
if (grade < 60){
alert ("F");
}
else if (grade >= 60 && grade < 70){
alert ("D");
}
else if(grade >= 70 && grade < 80);{
alert ("C");
}
else if (grade >= 80 && grade < 90){
alert ("B");
}
else if (grade >= 90){
alert ("A");
}
}
var inputGrade = prompt("Enter a grade:");
var parsedInt = parseInt(inputGrade);
var finalGrade = calculateGrade(parsedInt);
alert(finalGrade);
I recently created a grade calculator using JavaScript... but the script is not working!
the problem is.. when I enter a number like "55" the result is showing in three dialouge box one is saying " you got A+ " second is " you got A" and showing all the grades i add into the code
the following code is
var name= prompt("Enter your name");
var number= prompt("Enter your number");
alert("Hello , " + name);
if ( number >= 80 ); {
alert("Hello you got A+");
}
if ( number >= 70 ); {
alert("Hello you have got A");
}
if ( number >= 60 ); {
alert("Hello you have got A-");
} if (number >= 50 ); {
alert("Hello you have got B"); }
can any one help me? whats wrong ? really sorry for my bad english!!
You should not be placing ; after your if statements. Remove them.
if (number >= 50 ); {
^
Also, I'm guessing that you only want one of those statements to run depending on the number entered? You could try using else if's if you only want one of those statements to run. Also, use parseInt() on your number prompt.
Here's a working jsFiddle.
var name= prompt("Enter your name");
var number= parseInt(prompt("Enter your number"));
alert("Hello , " + name);
if (number >= 80) {
alert("Hello you got A+");
}
else if ( number >= 70) {
alert("Hello you have got A");
}
else if ( number >= 60) {
alert("Hello you have got A-");
}
else if (number >= 50) {
alert("Hello you have got B");
}
For calculating correct grade you should change your code as changing condition as following:
var name= prompt("Enter your name");
var number= prompt("Enter your number");
alert("Hello , " + name);
if ( number >= 80 ) {
alert("Hello you got A+");
}
if ( number >= 70 && number < 80) {
alert("Hello you have got A");
}
if ( number >= 60 && number < 70 ) {
alert("Hello you have got A-");
} if(number >= 50 && number < 60) {
alert("Hello you have got B"); }
The number needs to be parsed into integer before using. By default it is string.
var number= parseInt(prompt("Enter your number"));
Then on all if statements you are having ";". Remove them. The code will work fine.
window.onload = function() {
var name= prompt("Enter your name");
var number= parseInt(prompt("Enter your number"));
alert("Hello , " + name);
if ( number >= 80 ) {
alert("Hello you got A+");
}
if ( number >= 70 ) {
alert("Hello you have got A");
}
if ( number >= 60 ) {
alert("Hello you have got A-");
} if (number >= 50 ) {
alert("Hello you have got B"); }
}
try using "else if" instead of just "if" for the alternative options. If the score is larger than the first option it will also be larger than all of the others so if will return "true" for all cases. In addition as stated by Zenith remove the ; after the "if" conditions and ideally use parseInt() to ensure that you are comparing numbers as suggested by Kirubhananth Chellam.
var name= prompt("Enter your name");
var number= prompt("Enter your number");
alert("Hello , " + name);
if ( number >= 80 ) {
alert("Hello you got A+");
}
else if ( number >= 70 ) {
alert("Hello you have got A");
}
else if ( number >= 60 ) {
alert("Hello you have got A-");
}
else if (number >= 50 ) {
alert("Hello you have got B");
}