Last active
August 17, 2016 04:03
-
-
Save mmeigooni/27ca4eb3dc2a63a52653fddd65e8f531 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
W1D2 Exercises | |
*/ | |
/** SECTION ONE - Booleans Conditionals */ | |
/** | |
EXERCISE ONE | |
1. Type the two boolean values -- true and false -- into your console. | |
2. Use the console to accomplish the following: | |
- Write an expression using > that will evaluate to false | |
- Write an expression using > that will evaluate to true | |
- Write an expression using < that will evaluate to false | |
- Write an expression using < that will evaluate to true | |
- Write an expression using two numbers and === that will evaluate to true | |
- Write an expression using two numbers and === that will evaluate to false | |
- Write an expression using two strings and === that will evaluate to true | |
- Write an expression using two strings and === that will evaluate to false | |
*/ | |
/** | |
EXERCISE TWO | |
Fill in the ??? with the following operators or values to make the statements output | |
the expected Boolean value. | |
*/ | |
12 ??? 78 | |
// => true | |
24 ??? 16 | |
// => false | |
45 !== ??? | |
// => true | |
"45" ??? 45 | |
// => false | |
"6" ??? "six" | |
// => true | |
/** | |
EXERCISE THREE | |
Write a function 'oldEnoughToDrink' that takes an 'age' as an argument and returns | |
true if the person with that age is old enough to drink. | |
*/ | |
/** | |
EXERCISE FOUR | |
There's an easy way to figure out how long a string is by adding .length to the | |
end of it. Try this out in the console: | |
*/ | |
"hello".length; | |
"".length; | |
"John Doe".length; | |
/** | |
Write a function 'sameLength' that accepts two strings as arguments, and returns | |
true if those strings have the same length, and false otherwise | |
*/ | |
/** | |
EXERCISE FIVE | |
Write a function 'passwordLongEnough' that accepts a "password" as a parameter | |
and returns true if that password is long enough -- you get to decide what | |
constitutes long enough. | |
*/ | |
/** | |
EXERCISE SIX | |
1. Write a function bouncer that accepts a person's name and age as arguments, and | |
returns either "Go home, NAME.", or "Welcome, NAME!" (where NAME is the parameter | |
that represents the person's name) depending on whether or not the person is old | |
enough to drink. | |
2. Write a function max that takes two numbers as arguments, and returns the | |
larger one. | |
3. Write a function min that takes two numbers as arguments, and returns the | |
smaller one. | |
4. Write functions larger and smaller that each accept two strings as arguments, and | |
return the larger and smaller strings, respectively. | |
*/ | |
/** | |
EXERCISE SEVEN | |
Write the following functions that each accept a single number as an argument: | |
- even: returns true if its argument is even, and false otherwise. | |
- odd: the opposite of the above. | |
- positive: returns true if its argument is positive, and false otherwise. | |
- negative: the opposite of the above. | |
*/ | |
/** | |
EXERCISE EIGHT | |
A couple of other useful built-in mathematical functions are Math.random, | |
Math.floor and Math.ceil. Look these functions up on MDN to learn how they | |
work, and use them to implement the following functions: | |
- randInt: Should accept a single numeric argument (n), and return a number | |
from 0 to n. | |
- guessMyNumber: Should accept a single numeric argument and compare it to a | |
random number between 0 and 5. It should return one of the following strings: | |
-- "You guessed my number!" if the argument matches the random number. | |
-- "Nope! That wasn't it!" if the argument did not match the random number. | |
*/ | |
/** SECTION TWO - More Conditionals */ | |
/** | |
EXERCISE ONE | |
What is wrong with the following definitions of square? Write a sentence or two describing the issue(s); then, | |
*/ | |
/** | |
EXERCISE ONE | |
1. Is the ! operator a unary operator, or binary operator? | |
2. Evaluate each of the following expressions first on a whiteboard or piece of | |
paper, and then in a console: | |
*/ | |
!(2 >= 2) | |
!(4 === 4) | |
!(5 !== 5) | |
1 > 2 || 2 > 2 || 3 > 2 | |
5 < 5 || 75 < 74 | |
/** | |
EXERCISE TWO | |
1. Write a function called 'scoreToGrade' that accepts a number as a parameter | |
and returns a string representing a letter grade corresponding to that score. | |
For example, the following grades should be returned given these scores: | |
'A' > 90 | |
'B' >= 80 | |
'C' >= 70 | |
'D' >= 60 | |
'F' < 60 | |
*/ | |
function scoreToGrade(score) { | |
// TODO: your code here | |
} | |
scoreToGrade(95); // => 'A' | |
scoreToGrade(72); // => 'C' | |
/** | |
Modify the 'scoreToGrade' function so that it returns | |
'INVALID SCORE' if the score is greater than 100 or less than 0. | |
*/ | |
/** | |
EXERCISE THREE | |
1. Think of at least three activities that you enjoy doing outdoors and the range | |
of temperatures and weather patterns (e.g sunny, windy, snowy, rainy, etc.) that | |
are best for these activities. Write a function 'whatToDoOutside' that accepts a | |
temperature and condition as parameters and outputs a string of the | |
format: "The weather is ideal for: ACTIVITY" (where ACTIVITY is an actual | |
activity). Make sure to include an else that indicates what should be done if | |
the conditions do not match any activities. If you're short on inspiration, | |
here are some ideas: | |
- Snow Sports: snowboarding, skiing | |
- Water Sports: surfing, sailing, paddle boarding, swimming | |
- Team Sports: basketball, baseball, football (American or everywhere else), etc. | |
2. The 'guessMyNumber' function from SECTION ONE EXERCISE EIGHT (line 116) the | |
accepts a guess n and checks it against a random number from 0 to 5 -- if the | |
guess n is greater than 5, output a different message indicating that the guess | |
is out of bounds. | |
NOTE: It will be helpful to first write a 'randInt' function that accepts a | |
number n and computes a random integer from 0 to n; then, you can use this | |
function in 'guessMyNumber'. | |
3. Modify the scoreToGrade function so that it returns 'A+/A-' for scores | |
of 98-100/90-92 respectively. Apply the same logic for all other letter grades. | |
*/ | |
/** | |
EXERCISE FOUR - ADVANCED | |
You should have noticed a large amount of repetitive code when modifying | |
'scoreToGrade' to accommodate + or - grades. When we do lots of repetitive | |
things, that's a clear signal that there's a better way. Write a helper | |
function 'letterGrade' that accepts two arguments, letter and score, and | |
works as follows: | |
*/ | |
function letterGrade(letter, score) { | |
// your code here | |
} | |
// These are examples of what a *working* function would output. | |
letterGrade('A', 95); // => 'A' | |
letterGrade('A', 91); // => 'A-' | |
letterGrade('B', 88); // => 'B+' | |
letterGrade('monkey', 160); // => 'monkey-' | |
/** Finally, use 'letterGrade' to remove the repetition in scoreToGrade. */ | |
/** | |
EXERCISE FIVE - ADVANCED | |
It turns out that we can write logical and and logical or in terms of each | |
other and logical not using De Morgan's Laws (https://en.wikipedia.org/wiki/De_Morgan%27s_laws). | |
1. Write a function or that works like ||, but only uses ! and &&. | |
2. Write a function and that works like &&, but only uses ! and ||. | |
*/ | |
/** SECTION THREE - INTRO REPETITION */ | |
/** | |
EXERCISE ONE | |
Summation to n: Let's implement the function sum that takes a single | |
parameter n, and computes the sum of all integers up to n starting from | |
0, e.g.: | |
*/ | |
function sum(n) { | |
// TODO: your code here | |
} | |
sum(3); // => 3 + 2 + 1 + 0 => 6 | |
sum(4); // => 4 + 3 + 2 + 1 + 0 => 10 | |
sum(5); // => 5 + 4 + 3 + 2 + 1 + 0 => 15 | |
// HINT: We can rephrase "the sum of n" as "n plus the sum of n - 1". | |
/** | |
EXERCISE TWO | |
Factorial of n: The factorial of n is the product of all the integers | |
preceding n, starting with 1, e.g.: | |
*/ | |
function factorial(n) { | |
// TODO: your code here | |
} | |
factorial(3); // => 3 * 2 * 1 => 6 | |
factorial(4); // => 4 * 3 * 2 * 1 => 24 | |
factorial(5); // => 5 * 4 * 3 * 2 * 1 => 120 | |
// Implement the factorial function by observing that the "factorial of n" | |
// can be rephrased as "n times the factorial of n - 1". | |
/** | |
EXERCISE THREE | |
Repeating a String n Times: Let's write a function called repeatString that takes | |
two parameters: a string str, which is the string to be repeated, and | |
count -- a number representing how many times the string str should be | |
repeated, e.g. | |
*/ | |
function repeatString(str, count) { | |
// TODO: your code here | |
} | |
repeatString('dog', 0); // => '' | |
repeatString('dog', 1); // => 'dog' | |
repeatString('dog', 2); // => 'dog' + 'dog' => 'dogdog' | |
repeatString('dog', 3); // => 'dog' + 'dog' + 'dog' => 'dogdogdog' | |
/** | |
Your task is to implement the repeatString function using the observation that | |
to repeat a string some count, we can concatenate that string onto the result | |
of repeating the string count - 1. | |
HINT: Observe that repeatString('dog', 0) should yield the empty string, ''. What | |
happens if you evaluate this: '' + 'dog'? | |
*/ | |
/** | |
EXERCISE FOUR | |
Compute the nth Fibonacci Number: The fibonacci numbers are represented by | |
the following sequence: | |
fib(n): 1 1 2 3 5 8 13 21 | |
| | | | | | | | | |
n: 0 1 2 3 4 5 6 7 | |
That is, fib(0) is 1, fib(1) is 1, fib(2) is 2, fib(3) is 3, fib(4) is 5, etc. | |
Notice that each fibonacci number can be computed by adding the previous two | |
fibonacci numbers, with the exception of the first two: fib(0) and fib(1). | |
More succinctly, | |
fib(0) is 1 | |
fib(1) is 1 | |
fib(n) is fib(n - 1) + fib(n - 2) | |
Write a function called fib that accepts a number n as a parameter and computes | |
the nth fibonacci number using the above rules. | |
*/ | |
/** | |
EXERCISE FIVE | |
Modify your sum function from EXERCISE ONE of this section to accept two | |
parameters, start and end: sum should now compute the sum of the numbers from | |
start to end, e.g. | |
*/ | |
function sum(start, end) { | |
// TODO: your code here | |
} | |
sum(2, 7); // => 2 + 3 + 4 + 5 + 6 + 7 => 27 | |
sum(3, 5); // => 3 + 4 + 5 => 12 | |
// What happens if start is larger than end? Modify sum to check for this | |
// case and, when found, swap the start and end arguments. | |
/** | |
EXERCISE SIX | |
Write a function 'product' that works like sum, except it should compute | |
the product of the numbers from start to end. | |
HINT: Refactor your 'factorial' function from earlier to be implemented in terms | |
of 'product'. | |
*/ | |
/** | |
EXERCISE SEVEN | |
Let's pretend for a moment that JavaScript does not have the addition | |
operator + -- instead, it comes with two functions called 'inc' and 'dec' | |
that perform increment and decrement respectively: | |
*/ | |
// ignore the fact that inc makes use of + | |
function inc(x) { | |
return x + 1; | |
} | |
function dec(x) { | |
return x - 1; | |
} | |
/** | |
Your task is to write a function called add that takes two numbers as | |
parameters, x and y, and adds them together. The catch is that you can only | |
use 'inc' and 'dec' to accomplish this. | |
*/ | |
/** | |
EXERCISE EIGHT | |
Write a function called 'isEven' that, given a number n as a parameter, returns | |
true if that number is even, and false otherwise; however, you need to do this | |
without using the % operator. | |
*/ | |
/** | |
EXERCISE NINE | |
Write a function called multiply that accepts two numbers as parameters, and | |
multiplies them together -- but without using the * operator; instead, you'll | |
need to use repeated addition. | |
*/ | |
/** | |
EXERCISE TEN - ADVANCED | |
By now you should have worked with the length property of strings, | |
e.g. "hello".length. Your task is to write a function called stringLength | |
that accepts a string as a parameter and computes the length of that string; | |
however, as you may have guessed, you are not allowed to use the length | |
property of the string! | |
Instead, you'll need to make use of the string method called slice. To get an | |
idea of how slice works, try the following at a console: | |
*/ | |
"hello".slice(0); | |
"hello".slice(1); | |
"".slice(1); | |
/** | |
For our purposes, we can consider slice as taking one argument -- the index to | |
begin slicing from, and returns a new string starting from that index onwards. | |
Indices are positions of characters within strings, and they always begin counting | |
from 0, e.g.: | |
"h e l l o" (spaces added for clarity) | |
| | | | | | |
0 1 2 3 4 | |
The "h" character has index (position) 0 in the string "hello", "e" has | |
index 1, l has index 2, etc. | |
*/ | |
/** | |
EXERCISE ELEVEN - ADVANCED | |
The "modulo" operator (%) computes the remainder after dividing its left | |
operand by its right one, e.g. | |
*/ | |
5 % 2; // => 1 | |
8 % 10; // => 8 | |
7 % 5; // => 2 | |
/** | |
Write a function called modulo that works like the % operator, but without using it. | |
*/ | |
/** | |
EXERCISE TWELVE - ADVANCED | |
Write a function called countChars that accepts two parameters: a string and a | |
character. This function should return a number representing the number of times | |
that the character appears in string. To access the first element of a string, you | |
can use the following syntax: | |
*/ | |
// access the element at index 0 | |
"hello"[0]; // => "h" | |
"dog"[0]; // => "d" | |
// HINT: You'll also need to make use of the slice method as shown above in the | |
// exercise on computing the length of a string. | |
/** | |
EXERCISE THIRTEEN - ADVANCED | |
Implement a function called indexOf that accepts two paramters: a string and a | |
character, and returns the first index of character in the string. You'll need | |
to make use of the techniques for accessing the first element of a string and the | |
rest of the string (slice) as before. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment