Penguins JS Lessons

Download Homework
Submission Google Form for Homework

Mathematical Expressions Homework

In order to learn this subject of programming well, you have to practice:

Popcorn hack 1: operations with variables

Popcorn hack 2: Make 2 variables x, and y. Compute 3 different operations and log it into the console with console.log();

Final Task: Build a system of functions that represent different mathematical expressions. Each of these functions will perform a different mathematical operation(ex: add, subtract). Make at least 5 of these different functions.


Popcorn Hack 1 (Progress Check)

Part 1:
In the javascript node below, make 2 variables, num1 set to 6 and num2 set to 3, then write the equation (num1 * num2) + (num1 / 2).

The output should be 21.

Part 2:
Next, keeping the same variables, add a new variable called total that is equal to the equation num1 / num2 + 2 ** 3, and this time make the equation

(total + 15) - numb1 ** 2 / 4

Write this code below:

// put your popcorn hack 1 code here:



Popcorn Hack 2 (Progress check):

Create 2 variables x and y. With the output area below, provide 3 different operators used with the 2 variables
(ex: let sum = x + y;)
and fill in the output with a sentence that provides all three operators and their respective answer.

%%html

<h3>Popcorn hack 2 output</h3>
<div id="homework1"></div>
<script>    
(() => {
    // Your variables go here
    let x = 
    let y = 

	// put your operations here:
	// feel free to do any operation and change the names of the variables
	let operation1 = 
	let operation2 =
	let operation3 = 

    // Put the answer for the 3 operators in this DOM element
    document.getElementById("homework1").innerText = 
})();
</script>

Popcorn hack 2 output


Final Task:

  1. Make 5 different functions, each with different operators/expressions:
  • Addition
  • Subtraction
  • Multiplication
  • Exponential
  • Modulus
  1. Be able to use the functions as well as document.getElementById to make the buttons output the result.

  2. Have fun! Mess around with different variables with different values, and maybe see if you can make any complex equations like the ones you’re learning in your math class. The end objective is for you to be able to use and understand math expressions.

Example function:

function add(a, b)
{
	return a + b;
}

Some ideas you can use:

  • Multiply Function
  • Square root function
  • Cube root function

Now that you have seen the example, you will start writing your custom functions in the html code block below:
Make sure to follow each comment on where to write your code, don't modify any existing code.

%%html

<!-- This is where the outputs of your functions will go: -->
<h2>Function outputs:</h2>


<!-- Modify each of the button onclick fields to call your functions: -->
<!-- Make sure when the function is called you input your parameters. -->
<!-- (Optional) You can also change the text each button displays if you want. -->
<button type="button" onclick="Add(4,5)">Function Output 1</button>
<div id="functionOutput1"></div>
<br>

<button type="button" onclick="Expression()">Function Output 2</button>
<div id="functionOutput2"></div>
<br>

<button type="button" onclick="ADD YOUR FUNCTION HERE">Function Output 3</button>
<div id="functionOutput3"></div>
<br>

<button type="button" onclick="ADD YOUR FUNCTION HERE">Function Output 4</button>
<div id="functionOutput4"></div>
<br>

<button type="button" onclick="ADD YOUR FUNCTION HERE">Function Output 5</button>
<div id="functionOutput5"></div>
<br>


<!-- You will write the code for your functions in this script area  -->
<script>
	//Example function (you can use this if you want):
	function Add(a,b)
	{
		let result = a + b;
		
		// This line is important. It is what changes the output text section to the result of the function
		document.getElementById("functionOutput1").innerText = result;
	}

	// Example function template
	function Expression() {
		let result = 
		document.getElementById("").innerText = result;
	}


	// Add all of your own functions here:
	// You can make them do any mathematical expression, just make sure they change the text of one of the output sections
</script>


Function outputs:







Grading

Popcorn Hack 1: Completion gives 0.2 points.

Popcorn Hack 2: Completion gives 0.3 points.

Final Task: Completion gives 0.5 points. Max of 0.02 extra points from extra work relevant to the subject or finishing assignment in a way that shows exceptional comprehension of the lesson.


Make sure to submit your homework deployed link onto the global homework submission google sheet

Submission Google Form for Homework