Wednesday, April 13, 2011

Class Lab / Notes

We will be writing code that makes decisions and comparisons.
The tendency of new programmers is to dive immediately into the syntax vs. outlining a process or approach.

Pseudo Code is your friend.

Making Decisions
Conditional Statements : If Statements

if(the condition to be met is true) {
                 then do this }
else {
               do this instead}

To test multiple conditions:   if/else if/else statement
if(the condition to be met is true) {
                 then do this }
else if (condition to be met){
                or do this instead}
else  {
              or if not that then do this}

Comparison Operators
==  this is for Equality
>= greater than equal to
>  greater than
!= not equal

Logical Operations
&&  AND
|| OR (this is the pipes key which is the key above enter, the same key as \ )

if (conditionA && conditionB) {
    //do this stuff
This means that both parts have to be true for the whole thing to be true.

 If you are using && put the condition that is most likely false first, this stream lines the code because if the first part of the && is not true then it doesn't bother reading the rest.


var input = prompt('How much money do you have to spend on lunch at Disneyland?');

var cash = Number(input);

if (cash == 2 &&  cash < 3 ) {
    document.write("Vending Machine Soda") }
else if (cash >= 3 && cash < 5 ) {
    document.write("You can get a Churro") }
else if (cash >=5 && cash < 10) {
    document.write("Kids Hamburger from Tomorrowland Terrace") }
else if (cash >=10 && cash < 20) {
    document.write("Chicken Basket from Frontierland")}
else if (cash >=20) {
    document.write("Goofy's Kitchen")}
else if (cash <2) {
    document.write ("get more money")}
else  {
    document.write ('<p>You entered '  + input + ' You did not enter the dollar amount correctly.</p><p>For example: If you have $5.22, please simply enter 5.22')}

No comments:

Post a Comment