# Tutor profile: Hans B.

## Questions

### Subject: Python Programming

Write a Python function called `fizzbuzz` which takes no parameters, and prints out all the numbers between 1 and 100. However, for every number evenly divisible by 3, print "Fizz" instead of the number, for every number evenly divisibly by 5, print "Buzz" instead of the number, and for every number evenly divisible by both 3 and 5, print "FizzBuzz" instead of the number.

Let's start with the function header. To declare a function in Python, we use the `def` keyword, followed by the function name, and all its parameters between two parentheses. Since there are no parameters for this function, we will provide an empty set of parentheses. At the end of the function header, we need the function delimiter, which is a colon ':'. def fizzbuzz(): Now let's think of what the function body will look like. We know we'll be going through every number from 1 to 100. This will require a for ... in loop, as we are iterating through every number from 1 to 100. We can use a function called `range(<lower_bound>, <upper_bound>)` that can give us a an iterable of numbers from [lower_bound, upper_bound). Note that upper_bound is excluded, so if we do range(1,100) – we are actually excluding 100! So let's make this a range(1,101). Now let's iterate through this range with a for ... in loop, with our iterating variable to be named `i`. def fizzbuzz(): for i in range(1,101): Now we need to think of the body of the for ... in loop. We know that the variable `i` will store a number. We just need to print different things based upon what the number is and what it's divisible by. This means we have certain conditions to print numbers under. Which means we will be using `if`, `elif`, and `else`. So let's think of the first case – if the number is divisible by 3. If it is, let's print "Fizz". The way we determine if a number is divisible by another number is if its remainder is 0. To calculate the remainder (or mod) of `i` divided by 3, we need to do `i % 3` and check that it is equal to 0. This is a boolean evaluation, so if it evaluates to `True`, the code block within the conditional will execute. def fizzbuzz(): for i in range(1,101): if i % 3 == 0: print("Fizz") Now let's do the same, except with `i` divided by 5. We will use an `elif` to continue this branch of conditionals. def fizzbuzz(): for i in range(1,101): if i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") Let's do this again, except now if `i` is divisible by 3 and 5. Let's use the `and` keyword between these two boolean evaluations. They both need to evaluate to `True` for the whole condition to be `True`: def fizzbuzz(): for i in range(1,101): if i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") elif i % 3 == 0 and i % 5 == 0: print("FizzBuzz") Are we done? Nope! If a number is not divisible by either 3 or 5, we just print that number. This requires us to use the `else` keyword. If no conditions prior to `else` successfully evaluate to `True`, then all the code within `else` will execute. So let's print `i`! def fizzbuzz(): for i in range(1,101): if i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") elif i % 3 == 0 and i % 5 == 0: print("FizzBuzz") else: print(i) And that's how you do fizzbuzz! Right? Actually this implementation is wrong! The reason why is because "FizzBuzz" will never print. Let's say we have the number 15. 15 is divisible by 3, right? So let's print "Fizz". Wait... that's not right! The reason it's printing "Fizz" and not "FizzBuzz" is because even though it's technically divisible by both 3 and 5, the condition where it's divisible by 3 evaluates to `True`, so any other branches among these conditions will not even be executed. So how do we fix this? Let's put more specific cases (divisible by both 3 and 5) first! def fizzbuzz(): for i in range(1,101): if i % 3 == 0 and i % 5 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i) Now this is a successful implementation of FizzBuzz!

### Subject: Javascript Programming

Pretend that you are writing some JavaScript code for a checkout page. Right now, this checkout page is incomplete – we want to calculate the total amount at checkout from the subtotal and a given tax rate. Let's assume that there exists an HTML document with a <span> element, with an attribute `id` of value `subtotal`, storing the value of the subtotal (i.e. <span>3.99</span>). Let's also assume that there exists a <span> element, with an attribute `id` of value `total`. Write some JavaScript code that will calculate the total amount, and display this total amount for the user to see. Assume that there exists a variable with the name `TAX_RATE` that you can use to calculate how much tax is going to cost.

Let's think of any calculations we'll need for this code. We're calculating the `total`, which is subtotal + tax. $( \text{total} = \text{subtotal} + \text{tax} $) And we calculate $$\text{tax}$$ as $$ \text{tax} = \text{subtotal} * \text{TAX_RATE} $$, so the equation is: $( \text{total} = \text{subtotal} + \left(\text{subtotal} * \text{TAX_RATE}\right) $) So the first step is to get the subtotal. Before we get the subtotal, we need to get the element that the subtotal exists within. Luckily, we can do that with a function: `document.getElementById(...)`, where we provide the id of the element we are trying to fetch. We know we want the <span> element that has the id of `subtotal`, so let's write that first line. document.getElementById(subtotal); What's wrong with this line? A few things actually. First, document.getElementById(...) only accepts arguments that are strings, so we need to provide `subtotal` as a string (using either single-quotes or double-quotes): document.getElementById("subtotal"); There's still something wrong. We need to use a variable! `document.getElementById(...)` fetched the element from HTML, but we're not storing it anywhere. That means we'll lose reference to this HTML element! So let's store it in a variable called `subtotalElement`. To declare a variable, we use the syntax `let variableName = ...` let subtotalElement = document.getElementById("subtotal"); So how do we get the actual subtotal out of this element? Luckily for us, an HTML element has an attribute called `innerText`, so we just need to call this attribute from our subtotalElement. Just so that we can use it again, let's store it in a variable called `subtotal`. let subtotalElement = document.getElementById("subtotal"); let subtotal = subtotalElement.innerText; Then, let's calculate the tax by multiplying subtotal by the TAX_RATE variable that we can assume exists. Let's store the result of this in a variable called `tax. let subtotalElement = document.getElementById("subtotal"); let subtotal = subtotalElement.innerText; let tax = subtotal * TAX_RATE; Something's wrong here. Technically, `subtotal` isn't a number! It's a string! When we call `.innerText` from an HTML element, it gets returned as a string. We cannot perform any calculations between a number and a string, so we need to convert `subtotal` to a number first. We want it to be float, so let's use the `parseFloat(...)` function: let subtotalElement = document.getElementById("subtotal"); let subtotal = parseFloat(subtotalElement.innerText); let tax = subtotal * TAX_RATE; Now that we correctly calculate everything, let's calculate `total` by adding together `subtotal` and `tax`! let subtotalElement = document.getElementById("subtotal"); let subtotal = parseFloat(subtotalElement.innerText); let tax = subtotal * TAX_RATE; let total = subtotal + tax; Now that we have the total, let's display this on the page! We know we want to display this calculated value in a <span> element with id `total`, so let's fetch that element and store it in a variable called `totalElement`. let subtotalElement = document.getElementById("subtotal"); let subtotal = parseFloat(subtotalElement.innerText); let tax = subtotal * TAX_RATE; let total = subtotal + tax; let totalElement = document.getElementById("total"); Now all we have to do is display the total within totalElement. Let's modify the `innerText` attribute by setting its value to `total`. let subtotalElement = document.getElementById("subtotal"); let subtotal = parseFloat(subtotalElement.innerText); let tax = subtotal * TAX_RATE; let total = subtotal + tax; let totalElement = document.getElementById("total"); totalElement.innerText = total; And that's how you display a calculated total on a checkout page!

### Subject: Pre-Calculus

Find the point of inflection for $$y=x(x-3)^2$$.

The point of inflection is considered the point where a curve goes from concave up to concave down, or vice-versa. We consider a point to be concave up if it is curved upwards, meaning that $$y'' > 0$$. We consider a point to be concave down if it is curved downwards, meaning that $$y'' < 0$$. The point of inflection is where $$y'' = 0$$, or where $$y''$$ crosses from positive to negative or vice-versa. To find this, we must calculate $$y''$$. Before calculating $$y''$$, we must simplify $$y$$. $$y = x(x-3)^2$$ Let's simplify $$(x-3)^2$$ to become $$(x-3)(x-3)$$, when we can then simplify to be $$(x^2-6x+9)$$. $$y = x(x^2-6x+9)$$ Then we distribute $$x$$. $$y = x^3 - 6x^2 + 9x$$ Then let's calculate $$y'$$ by taking the derivative of $$y$$. $$y'=3x^2-12x+9$$ Then let's calculate $$y''$$ by taking the derivative of $$y'$$. $$y''=6x-12$$ Now all we have to do is set $$y'' = 0$$ and solve for $$x$$. $$0 = 6x-12$$ $$6x = 12$$ $$x=2$$ So now we know the point of inflection is $$x=2$$.

## Contact tutor

needs and Hans will reply soon.