Java Script Arrays Conditionals Using Random Number Option

Java. Script Arrays Conditionals

Using Random Number

Option 1 Get a random number Convert to an index Index into an array

Option 2 Get a random number Make a decision based on the value

Conditionals Test if a Boolean expression is true or false Allows a program to make a choice

IF … THEN … ELSE if (MUST BE LOWERCASE) no else: do nothing if not true else: one or the other else if: series of choices

Boolean expressions Operator Meaning == Is equal to != Is not equal > Is greater than < Is less than >= Is greater than or equal to <= Is less than or equal to Creates a Boolean value from numbers or strings

Grades Example Let’s assign letter grades to students! 90 - 100 – A 80 - 89 –B 70 - 79 –C 60 - 69 –D <60 –F

Grades Example With an Array 0 – 100 0 F Collapse all of the bottom 60 1 D Subtract 60 (grade-60) 2 C All negative numbers to 0 (max(0, grade) 3 B 4 A 0 – 40 Divide by 10 0 – 4 (but not an integer) Floor 0 – 4 (integer)

Easier to just make decisions!

Decision Tree (one of several) grade < 60 F grade < 70 grade < 80 D grade < 90 C B A

In Java. Script if (num < 60) { grade = "F"; } else if (num < 70) { grade = "D"; } else if (num < 80) { grade = "C"; } else if (num < 90) { grade = "B"; } else { grade = "A"; }

In Java. Script if (num < 60) { grade = "F"; } else if (num < 70) { grade = "D"; } else if (num < 80) { grade = "C"; } else if (num < 90) { grade = "B"; } else { grade = "A"; } Java. Script statement(s)

Lab random selection uneven probability
- Slides: 14