1

So I am taking a Javascript course at my college, and I ran into two issues, the solutions to which, I've searched everywhere online for. I'll keep it short, because Im about to break my keyboard just thinking about this.

First difficulty:

var year = 2017;
var age = prompt("Please enter your age: ");
console.log("I was born in the year of " + (year - age));
var studying = prompt("Enter the number of years you expect to study in the college: ");
console.log("You will graduate from Seneca College in the year " + (year + studying));

Expected output:

1995

2019

Actual output:

1995

20172 //It seems to concatenate, regardless of how I try to add the year and studying together

Second difficulty:

function grader(x) {
    switch (x) {
        case (x < 50) :
            console.log('Grade: F');
                break;
        case (x >= 50) :
            console.log('Grade: D');
                break;
        case (x >= 60) :
            console.log('Grade: C');
                break;
        case (x >= 70) :
            console.log('Grade: B');
                break;
        case (x >= 80) :
            console.log('Grade: A');
                break;
        default :
            console.log('Error, invalid grade');
    }
}
console.log("The letter grade is: " + grader (40));

Expected output:

The letter grade is: Grade F

Actual output:

Error, invalid grade

The letter graded is: undefined

Any help would be appreciated. I have no idea why I am running into these errors.

displayName
  • 155
  • 1
  • 9

4 Answers4

2
year + +studying

(Add a number to a number, not a number to a string)


switch (x) {
    case (x < 50) :

is equal to:

if( x === x < 50 )

which equals:

if( 40 === true )

which is obviously false. Just do

switch (true) {
    case (x < 50) :

To make it work

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Thank you! Holy crap! One more thing, though. When I run the code, it says `Grade: F` and then on the next line it says `undefined`. Any thoughts? – displayName Sep 12 '17 at 19:00
  • @michael cause your console logs the result of the call which is undefined as grader does return nothing. – Jonas Wilms Sep 12 '17 at 19:01
2

For the first one, that can be solved with a simple parseInt(year) + parseInt(studying). Honestly, probably just a parseInt(studying) as I believe prompt returns a string.

For the second one, the answer to that lies here.

You can't do conditionals in switch case statements like you think.

Edit:

Since you're a beginner, I recommend looking at the documentation on prompt here

and the documentation on switch statments here.

Zackary Murphy
  • 421
  • 5
  • 10
2

First difficulty:

Call the parseInt function on year and studying to convert them to integers before adding them:

var year = 2017;
var age = prompt("Please enter your age: ");
console.log("I was born in the year of " + (year - age));
var studying = prompt("Enter the number of years you expect to study in the college: ");
console.log("You will graduate from Seneca College in the year " + (parseInt(year) + parseInt(studying)));

Second difficulty:

In this problem there are several mistakes. I will abstain from detailed examples from each since it would take breaking up your code several times, but here is a quick rundown:

  • The switch keyword is used to check through concrete values of a variable. That is, the expression inside the case(...) clause must be a value, while in your case these are if statements.

  • Your conditions overlap! Notice that if a grade is greater than 60, it is also necessarilly greater than 50. If it is greater than 80, it is definitely greater than 70, 60, 50 and so on... It means that any grade of 50 or greater will fall into the condition (x >= 50) and never hit the others - unlikely what you were trying to achieve.

  • Notice that you try to print the return value of the grader function, but it never in fact returns a value...

I suggest you take your time and research more about the syntax and constructs of the language relevant to your problem before posting a question - searching for and discovering the answers yourself is where all the learning happens :)

Good luck!

JonyVol
  • 366
  • 1
  • 9
1

Another logic problem you have is that even if your switch statement worked, it will return either F or D (since x is either < 50 or >= 50, no other conditions will be checked).

I suggest you use an if/elseif structure (as switch(true) isn't very useful) and restructure your logic to make it pick the right grade letter:

function grader(x) {
  if (x >= 80) {
    return 'A';
  } else if (x >= 70) {
    return 'B';
  } else if (x >= 60) {
    return 'C';
  } else if (x >= 50) {
    return 'D';
  } else {
    return 'F';
  }
}
James
  • 20,957
  • 5
  • 26
  • 41