Skip to content

Instantly share code, notes, and snippets.

@abalogun
Created August 17, 2016 02:48
Show Gist options
  • Save abalogun/0978ab48d1d8cc32a25fe0bb224c7db3 to your computer and use it in GitHub Desktop.
Save abalogun/0978ab48d1d8cc32a25fe0bb224c7db3 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 passes invalid parameter.Refernce error returned.Change parameter to x.
function square(fiv) {
return 5 * 5;
}
//Parameter must not pass a number.Syntax error returned for unexpected number.Change parameter to valid word.
function square("x") {
return "x" * "x";
}
//Parameter must not be a string character.When running the square function, the error returned was a syntax error(unexpected string).Change to valid word.
/**
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) {
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) {
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>!"
*/
/**
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