What does the following statement do?
x = y;
It gets an error
It tests whether the two variables are equal
It copies the value of y into x
y
x
It copies the value of x into y
In this course, variables will almost always get their values from
prompt
We want some code that will ask the user their name and greet them. Which of the following does that?
var user = alert("what's your name?"); prompt("Greetings, "+user);
var user = prompt("what's your name?"); alert("Greetings, "+user);
var user = prompt("what's your name?"); var greet = "Greetings, "+user; alert(greet);
var user = alert("what's your name?"); var greet = "Greetings, "+user; prompt(greet);
There's a pattern to that kind of code:
Not all of our code will show that pattern, but it often will, so it's worth stepping back to see the pattern.
age >= 18
if
if( age >= 18 ) { alert("Don't forget to vote!"); }
if( cash < 12 ) { alert("I'll have pizza"); } else { alert("I'll have the filet mignon, please."); }
if( people_in_party > 1 ) { if( cash < 12 ) { alert("let's have pizza; my treat!"); } else { alert("filet mignon is on me!"); } } else { alert("I'll just have the pizza and eat it alone...."); }
There are some conventional diagrams to help understand conditionals; these are known as flowcharts. The first form allows you to optionally skip a code block The second form allows you to choose one of two mutually exclusive code blocks
Suppose a variable called hour contains the hour of the day, using a 24-hour clock (0--23). What is the best way to greet someone?
hour
if hour < 12 { alert("Good morning!"); } else { alert("Good afternoon!"); }
if( hour < 12 ) { alert("Good afternoon!"); } else { alert("Good morning!"); }
if( hour < 12 ) { alert("Good morning!"); } if( hour > 12 ) { alert("Good afternoon!"); }
if( hour < 12 ) { alert("Good morning!"); } else { alert("Good afternoon!"); }
Note: A common beginner error is to test for both something and its opposite. For example, testing if a number is odd and then testing if it's even. Better is to test if it's odd, and if it's not, you know it's even, so you can just use the else keyword.
else
Suppose you've just used the prompt() function to ask the user for their name, and the value is stored in a variable called username. You want to see if they typed anything and complain if they didn't. Which of the following would complain appropriately? (The value "" refers to an empty string.)
prompt()
username
""
if( username = "" ) { alert("please type your name"); }
if( username.length < 1 ) { alert("please type your name"); }
if( username.length == "" ) { alert("please type your name"); }
if( username == "" ) { alert("please type your name"); }
Note:
=
==
What is the result of the following code? Note that you are allowed encouraged to use the JS console to find out!
var y = Math.floor(3.8);
3.8
4
3.5
3
What is the value of y as a result of the following code? Again, you are allowed encouraged to use the JS console to find out! We'll discuss this syntax (called a method a bit later in the course).
var x = 13; var y = x.toString(16);
13
a
d
Write some JS code that does the following:
parseInt
alert
We can use JavaScript to convert decimal values to hex values. This will give us some opportunities to use conditionals.
Write some JS code to does the following:
%
Once you've got that working, add a conditional to report whether the units digit will be a numeral (0-9) or a letter (A-F). Test it with your jsfiddle.
Add another conditional to report whether the hex string will be one digit or more than one digit. Can you think of how to do this? Test it with your jsfiddle.
After each step, test it with your jsfiddle.
We've implemented a function on this page, just for fun:
fallColors()
Implement some JavaScript code that prompts the user for their favorite season. If they answer "fall", invoke the fallColors() function. Note that you won't be able to test this using jsfiddle (because jsfiddle lacks the magic functions), but you can test it in your JS console. The point of this is to do a conditional using a string.
You might come up with something like this:
var season = prompt("What's your favorite season?"); if( season == "fall" ) { fallColors(); }
Will be posted later, visit again after .
Quiz answers:
1. C ; it copies into x, the left-hand side 2. B or C; either will work 3. D is best. C will also work but it's not as good 4. B or D will work. D is most common. 5. D ; Math.floor() computes the integer below the number 6. D ; .toString(16) converts to base 16.
Wellesley Class Year
var input=prompt("In what year did you enroll at Wellesley College?"); var year=parseInt(input); if (year>2017){ alert ("Welcome future Wellesley Student!") } else if (year >=2013){ alert ("You are a current student.") } else { alert ("Welcome back alumna!") }
Hex Conversions in JavaScript:
var entered = prompt("Please enter a decimal number"); var num = parseInt(entered); var units = num % 16; alert("The units digit is "+units);
Hex Conversions with Conditionals:
var entered = prompt("Please enter a decimal number"); var num = parseInt(entered); var units = num % 16; alert("The units digit is "+units); if( units > 9 ) { alert("digit will be a letter"); } else { alert("digit will be a numeral"); }
Hex with More Conditionals:
var entered = prompt("Please enter a decimal number"); var num = parseInt(entered); var units = num % 16; alert("The units digit is "+units); if( units > 9 ) { alert("digit will be a letter"); } else { alert("digit will be a numeral"); } if( num >= 16 ) { alert("result will be multiple digits"); }
Hex Conversions with 16s Place:
var entered = prompt("Please enter a decimal number"); var num = parseInt(entered); var units = num % 16; var sixteens = Math.floor(num/16); alert("The sixteens digit is "+sixteens); alert("The units digit is "+units); var hex = sixteens.toString(16) + units.toString(16); alert("the hex conversion is "+hex);