Skip to content

Instantly share code, notes, and snippets.

@mmeigooni
Last active August 17, 2016 02:22
Show Gist options
  • Save mmeigooni/fab92765a713be1a8d4448d52ccab7d0 to your computer and use it in GitHub Desktop.
Save mmeigooni/fab92765a713be1a8d4448d52ccab7d0 to your computer and use it in GitHub Desktop.
/**
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;
}
function square(5) {
return 5 * 5;
}
function square("x") {
return "x" * "x";
}
/**
EXERCISE TWO
Fix the invalid syntax in the following functions
*/
func square1(x {
return x * x;
}
functionsquare2 x)
return x * x;
}
function (x) square3 {
return x * x;
/**
EXERCISE THREE
The following functions exhibit poor style -- fix these issues. Use slide 67 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
}
/**
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() {
// your code here
}
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.
*/
/**
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>!"
*/
/**
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).
*/
/**
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
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment