Skip to the content.

3.3 Homework

Hack 1

Hack 1 (Python)

### Mathematical Functions ###
def add(a,b):
    return a + b # Adds A & B

def subtract(a,b):
    return a - b # Subtracts A & B

def divide(a,b): # Divides A & B
    return a / b

def modulus(a,b):
    return a % b # Divides A & B and retrieves remainder

def power(a,b):
    return a ** b # A is raised to the power of B

### Example Usage ###
print( add(6,3) ) # Output: 9
print( subtract(6,3) ) # Output: 3
print( divide(6,3) ) # Output: 2.0
print( modulus(6,3) ) # Output: 0
print ( power(6,3) ) # Output 216

9
3
2.0
0
216

Hack 1 (JavaScript)

/// Mathematical Functions ///

// Adds A & B
function add(a,b) { 
    return a + b
}

// Subtracts A & B
function subtract(a,b) {
    return a - b 
}

// Divides A & B
function divide(a,b) {
    return a / b
}

// Divides A & B and retrieves remainder
function modulus(a,b) {
    return a % b
}

// A is raised to the power of B
function power(a,b) {
    return a ** b
}


/// Example Usage ///
console.log( add(2,5) ) // Output: 7
console.log( subtract(2,5) ) // Output: -3
console.log( divide(2,5) ) // Output: 0.4
console.log( modulus(2,5) ) // Output: 2
console.log( power(2,5) ) // Output: 32

Hack 2

Hack 2 (Python)

# Takes parameter and multiples by 5 and adds 2
def point_finder(x):
    return (x*5) + 2

# Example Output
print(point_finder(-4)) # Output: -18
print(point_finder(5)) # Output: 27
print(point_finder(10)) # Output: 52
-18
27
52

Hack 2 (JavaScript)

// Takes parameter and multiples by 5 and adds 2
function point_finder(x) {
    return (x*5) + 2
}

// Example Output
console.log(point_finder(-4)) // Output: -18
console.log(point_finder(5)) // Output: 27
console.log(point_finder(10)) // Output: 52