Penguins - Mathematical Expressions JS Hacks
Exploring JavaScript and mathematical expressions.
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:
Task 1: Make 2 variables
x, andy. Compute 3 different operations and log it into the console with console.log();
Task 2: 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.
Task 1:
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>Homework Part 1</h3>
<div id="homework1"></div>
<script>
(() => {
// Your variables go here
let x = 2;
let y = 3;
let sum = x + y; // Addition
let product = x * y; // Multiplication
let power = x ** y; // Exponentiation
let answer = `Sum: ${sum}, Product: ${product}, Power: ${power}`;
// Put the answer for the 3 operators in this DOM element
document.getElementById("homework1").innerText = answer;
})();
</script>
Homework Part 1
Task 2:
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:
Final instructions
- Make 5 different functions, each with different operators/expressions:
- Addition
- Subtraction
- Multiplication
- Exponential
- Modulus
-
Be able to use the functions as well as
document.getElementByIdto make the buttons output the result. -
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.
Grading
Part 1: Completion gives 0.4 points. 0.01 point will be given if DOM sentence is grammatically correct
Part 2: 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.