Review Tasks (show me what you've got)
(In VSCode)
Make sure you've gone over the review of Gr. 11.
You've reviewed grade 11 skills:
Variables
If-Statements
While Loops
For Loops
Function Definitions
Arrays (and Strings)
You've prepared the computer:
Practiced using VSCode
It's time to code!
Read the instructions listed below.
Write the various functions.
Test your code.
Instructions:
First of all - READ FULLY and CAREFULLY
Create a new Workspace in VSCode by going to FILE > Open Folder
Make a new folder inside your home directory (/home/username)
Name the folder "Review of Gr 11"
Add a single file called main.js
In this code file write and test the following functions:
a) contains_uppercase(str)
Given the string str, return whether or not the string contains any uppercase letters (true or false).
b) contains_lowercase(str)
Given the string str, return whether or not the string contains any lowercase letters (true or false).
Hints: you will likely need to use the .toUpperCase() and .toLowerCase() functions. Although there are other ways to do it.
Note #1 - return values do not show on the console. If you want to see them, you'll need to print them.
For example: console.log(contains_uppercase("Hello World"));
or: let result = contains_lowercase("NOPE"); and then print result.
Note #2 - part of this course is writing more readable and short code. Each of the above functions has a very descriptive name. Try to use this style of code whenever you work from now on.
Note #3 - part of this course is writing short or more efficient code (those are two different things). The above functions can both be done in one line. Without Googling it - can you do them in one line?
2. pwd_check(password):
A password is considered secure if it follows all of these rules, in order:
Minimum 10 characters in length
Includes both uppercase and lowercase letters
Includes a number
Includes a special character (ie. not a letter or number)
Must not contain spaces
Your function, pwd_check(password), will return true if the given password passes all of these rules and false otherwise. Furthermore, if the return value is going to be false, it also prints to the console the (first) rule that was failed. Hint: print to the console before the return.
Examples:
pwd_check("HelloWorld!");
"Your password must contain a number."
false
pwd_check("password123!");
"Your password must contain an uppercase and lowercase letter."
false
pwd_check("PassWord 123!");
"Your password must not contain spaces."
false
pwd_check("Purple_Polkadot8");
true
Hints:
It might make your life easier to make helper functions like "contains_space(pwd)"
You should also familiarize yourself with the string .charCodeAt() function as well as the UTF-16 character table (specifically for letters, numbers, and space).
3. lotto649():
Ontario's Lotto 6/49 requires you to select six unique numbers from 1-49. If you match all 6 randomly chosen numbers - you win the lottery! Your job is to write the program that selects the 6 unique random numbers for the lottery and then print them to the screen in ascending order. As a precaution, your function should also return an array containing the selected numbers.
For Example:
lotto649()
4
21
22
34
41
44
[4,21,22,34,41,44] // That's the returned array which likely won't show on the console
Your program should not select the same number twice!
Hints: while-loop, for-loop, Math.random( ), Array.sort( )
4. dice_roll_stats(sides, rolls):
When you roll two dice, the probability of a certain sum depends on the number of sides on the dice. For example, two 6-sided dice have a 2.78% chance of snake-eyes (two 1's) or 12 (two 6's). There is a 16.67% chance of rolling a 7. But what if the dice have 8 sides each? 20?
You will write the function dice_roll_stats(sides, rolls) that takes the number of sides on the dice (minimum 4, maximum 20) and calculates experimental probabilities using the number of rolls given. The sides will be equal for both dice.
The function will "roll" two dice and keep track of the outcome. It will continue to do this rolls times. Afterward, the results are printed to the screen in a particular format.
First, the output will list the possible outcomes (separated by a tab "\t").
Then, the output will list the number of times each sum was recorded (separated by a tab "\t").
Finally, the output will list the probability (as a percent, rounded to two decimals) for each sum (separated by a tab "\t").
The output won't be pretty - wouldn't it be great if we could use HTML?
Example: dice_roll_stats(6, 10000)
2 3 4 5 6 7 8 9 10 11 12
285 570 818 1079 1392 1676 1444 1122 789 559 266
2.85% 5.7% 8.18% 10.79% 13.92% 16.76% 14.44% 11.22% 7.89% 5.59% 2.66%
dice_roll_stats(10, 10000)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
106 198 303 386 491 598 726 815 913 1015 941 767 696 557 489 404 277 202 116
1.06% 1.98% 3.03% 3.86% 4.91% 5.98% 7.26% 8.15% 9.13% 10.15% 9.41% 7.67% 6.96% 5.57% 4.89% 4.04% 2.77% 2.02% 1.16%
The examples above are actual output.