Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Prompt, if/else statements, and switch statement

 
Ranch Hand
Posts: 335
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on my next assignment, and again I can not get a anything to show....for the class we do have to use prompts per the instructions. Last time I was told they were obsolete, but it looks like we are sticking with them. I am sure that the prompts are correct, but they wont show now? Well if they were correct I guess they would show...in addition to that I have the following questions:

1) Am I using the else if statements correctly? The book uses that as an example if you have multiple statements, and just the normal if/else if you have two statements. So is that correct? Or would the else if's just be else's?

2) From my book example that is the correct set up for the switch statements, but I feel like im missing something. My final_grade is undefined until the if statements come into to play, which in my mind I do not see what's wrong with that. Of course somethings wrong or it would be working.

3) at the bottom I want the switch statements messages to follow the course_grade in the output. How would I do that? Would I just put it underneath the last document.write, or is there a way to make a call sort of speak to a switch statement?

 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:1) Am I using the else if statements correctly? The book uses that as an example if you have multiple statements, and just the normal if/else if you have two statements. So is that correct? Or would the else if's just be else's?


Close, but not quite. You need to change two things:
[list]The parens go around the whole if (or else if). So
not [/list]
  • There aren't parens after the else. That's the condition when none of the others match. You can put what you have in parens in a comment if you want to read it.


  • Cody Biggs wrote:2) From my book example that is the correct set up for the switch statements, but I feel like im missing something. My final_grade is undefined until the if statements come into to play, which in my mind I do not see what's wrong with that. Of course somethings wrong or it would be working.


    The switch is fine. It isn't getting that far because of the errors above.

    Cody Biggs wrote:3) at the bottom I want the switch statements messages to follow the course_grade in the output. How would I do that? Would I just put it underneath the last document.write, or is there a way to make a call sort of speak to a switch statement?


    Yes. You'd just reorder it in your code. A switch isn't a method so you can't just call it.
     
    Cody Biggs
    Ranch Hand
    Posts: 335
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So I made the changes, but my prompts still are not even showing up. All I get when I launch in a browser in the background turning grey, but no questions pop up.
     
    Sheriff
    Posts: 67618
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Be sure that the browser debugging console is open when you run your page. it will show you the errors.
     
    Cody Biggs
    Ranch Hand
    Posts: 335
    3
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh!!! I see what I did! I was thinking about the exam grades being entered when doing the if statements, and not the final grade....dang I don't know what I was thinking lol. Thank you everyone!

    Correct code is:


     
    Ranch Hand
    Posts: 90
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Cody,

    I recommend always using curly braces around the single-statement body of if/else/for/while statements if you are going to place the single statement on the next line. That is, instead of this:



    .. use this:



    The reason for always using curly braces is that there is less risk of confusion. Especially the way that you have the JavaScript code indented, it makes it look like the document.write() calls and switch statement are within the else block, but in fact they are not.

    Another approach is to place the single statement on the same line, like this:



    This also helps avoid the risk of confusion.

    Recently, there was an infamous security bug nicknamed "gotofail", which you can read about here: https://www.imperialviolet.org/2014/02/22/applebug.html

    The problem was that an extra goto fail; line was added, meaning that control would always go to the failure/clean-up code:



    .. because this is syntactically identical to:



    Maybe if curly braces were used around the if statements' single-statement bodies, then the error would not have occurred.

    Daniel
     
    Bear Bibeault
    Sheriff
    Posts: 67618
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Daniel is spot-on about the braces; never leave them off.

    Also, you might consider following industry standards for naming, using camel-case for variable names. So finalGrade rather than final_grade.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic