Created
August 17, 2016 02:47
-
-
Save hartbeatnt/846cf212089268e28552e8b12844596d 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
/** | |
Pair Programming Practice. Submit a link to your gist here when you are done: | |
https://goo.gl/forms/mPumIHpIKF3W1gjt2 | |
*/ | |
/** | |
EXERCISE ONE | |
What is wrong with the following definitions of square? Write a sentence or two describing the issue(s); then, | |
try copying the erroneous examples into a console one-at-a-time and observing the error(s) generated (you may | |
have to attempt to invoke the functions to see the error). What errors are produced (if any) for each erroneous | |
version? Do the errors make sense? | |
*/ | |
function square(monkey) { | |
return x * x; | |
} | |
//undefined | |
//change monkey to x | |
function square(5) { | |
return 5 * 5; | |
} | |
//Uncaught SyntaxError: Unexpected number | |
//parameter name can'tbe a number | |
function square("x") { | |
return "x" * "x"; | |
} | |
//Uncaught SyntaxError: Unexpected string | |
//can't multiply a string | |
/** | |
EXERCISE TWO | |
Fix the invalid syntax in the following functions | |
*/ | |
function square1(x) { | |
return x * x; | |
} | |
function square2 (x) { | |
return x * x; | |
} | |
function square3 (x) { | |
return x * x; | |
} | |
/** | |
EXERCISE THREE | |
The following functions exhibit poor style -- fix these issues. Use slide 63 from W1D1 Lecture as the "right" way to do it. | |
https://docs.google.com/presentation/d/1UD_oVyx8O-ELxk5pmsMDpCLptb7vpYrANCR2VA4_j2w/edit?usp=sharing | |
*/ | |
function square(x) { | |
return x * x; | |
} | |
function square(x) { | |
return x * x; | |
} | |
function square(x) { | |
return x * x; | |
} | |
/** | |
EXERCISE FOUR | |
Complete the function 'cube' that returns the cube of x: | |
*/ | |
function cube(x) { | |
// your code here | |
return x * x * x; | |
} | |
/** | |
EXERCISE FIVE | |
Complete the function 'fullName' that should take two parameters, 'firstName' and 'lastName', and returns the | |
'firstName' and 'lastName' concatenated together with a space in between. | |
*/ | |
// don't forget the parameters! | |
function fullName(firstName, lastName) { | |
// your code here | |
return firstName + " " + lastName; | |
} | |
fullName("John", "Doe") // => "John Doe" | |
/** | |
EXERCISE SIX | |
Write a function 'average' that takes two numbers as input (parameters), and returns the average of those numbers. | |
*/ | |
function average(num1, num2){ | |
return (num1 + num2) / 2; | |
} | |
/** | |
EXERCISE SEVEN | |
Write a function 'greeter' that takes a name as an argument and greets that name by returning something along the | |
lines of "Hello, <name>!" | |
*/ | |
function greeter(name){ | |
return "Hello, " + name + "!"; | |
} | |
/** | |
EXERCISE EIGHT | |
Compound interest can be calculated with the formula found here: | |
https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest | |
F: future value | |
P: present value | |
i: nominal interest rate | |
n: compounding frequency | |
t: time | |
Write a function 'futureValue' that can be used to calculate the future value of a quantity of money using | |
compound interest. | |
Use the function to calculate what the future value of $1700 (P = 1700) deposited in a bank that pays an | |
annual interest rate of 4.7% (i = 0.047), compounded quarterly (n = 4) after 6 years (t = 6) (you can | |
use Math.pow to do exponentiation). | |
*/ | |
function futureValue(P, i, n, t){ | |
return P * Math.pow(1 + i/n, n*t); | |
} | |
futureValue(1700, .047, 4, 6); | |
/** | |
EXERCISE NINE | |
Write your own square-root function called sqrt that accepts a number parameter and returns an approximate | |
square root. Square-root approximations make use of averages. Be sure to use the average function you | |
previously wrote. The first version of your square root function should perform no more than 3 successive | |
averages. Use this resource in case you're not familiar with ancient Babylonian algorithms: | |
http://www.had2know.com/academics/babylonian-algorithm-square-root.html | |
*/ | |
function average(num1, num2){ | |
return (num1 + num2) / 2; | |
} | |
function sqrt(number){ | |
var newNumber1 = average(number, number / 2); | |
var newNumber2 = average() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment