IF / ELSE IF |
Think of the elections.
Are you over 18? Or are you under 18? If you are over 18, you can vote. If not, the script stops. We will add more stuff in NESTED LOOPS Tutorial. Let's write a Javascript that simulates that.
|
Random Numbers |
Let's write a program on rolling the dice.
You will need Math.Floor and Math.random. Math.floor makes sure that you have a whole number and it narrows to the nearest number. On its own, math.floor gives you a decimal point. It is from 0 to 1 ... Math.random multiplies by 1 whatever number is set ParseInt() is var randomNum = Math.floor(Math.random()*6)+1; var guess = prompt ('What Number Are You Thinking About?'); if (parseInt(guess) === randomNum) { document.write('<p> You are correct! </p>'); } else { document.write ('<p> Sorry, the number is wrong</p>' + randomNum); } |
Example 1var total = 95.00;
var freeshipping = false; if (total >= 50.00) freeshipping = true; console.log(freeshipping); |
Example 2var total = 99.99;
var freeshipping = false; if (total >= 100.00) freeshipping = true; console.log("your shipping is " + freeshipping); |
Example 3var total = 90.00;
var freeshipping; if (total>= 90.00) freeshipping = true; else freeshipping = false; console.log (freeshipping); |
Example 4var total = 190.00;
var freeShipping; var savings; if (total >= 110.00){ freeShipping = true; savings = 90.00; } else { freeShipping = false; savings = 0; } console.log(savings); |
Example 5var orderType = 'business';
var shipMethod; if (orderType == 'business') shipMethod = 'FedEx'; else if (orderType == 'personal') shipMethod = 'UPS Method'; else shipMethod = 'USPS'; console.log(shipMethod); |
Example 6var orderType = prompt ("what is your shipping method? business, personal?", "");
var shipMethod; if (orderType == 'business') shipMethod = 'FedEx'; else if (orderType == 'personal') shipMethod = 'UPS Method'; else shipMethod = 'USPS'; console.log(shipMethod); |