Week 3: Ponder and Prove
Before beginning this Task:
- Make sure you have read and understood the reading for the week.
- Download the following Zip file and extract it into a local folder of your choice: week03.zip
- Open the folder created by unzipping week03.zip in VS Code
Getting values in HTML form control elements from JavaScript
In last week's task, you accessed HTML elements and set their content and attributes using JavaScript. In this task, you are to retrieve data entered by the user from the HTML form elements they used.
Use the same methods to access the HTML form elements that you used last week,
document.getElementById('formElementId')
or
document.querySelector('#formElementId'),
to retrieve the value of a form control element, we use the value property.
For example, to get a value from the following HTML form control with an ID of city:
<input type="text" id="city" name="city">
We would use the following syntax:
let city = document.querySelector('#city').value
Functions
Complete the following steps
In the task3.js file:
- Using function declaration, define a function named add that takes two arguments, number1 and number2
- In the function, return the sum of the parameters number1 and number2
- Using function declaration, define another function named addNumbers that gets the values of two HTML form controls with IDs of addend1 and addend2. Pass them to the sum function
- Assign the return value to an HTML form element with an ID of sum
- Add a "click" event listener to the HTML button with an ID of addNumbers that calls the addNumbers function
- Using function expressions, repeat Steps 1-5 with new functions named subtract and subtractNumbers and HTML form controls with IDs of minuend, subtrahend, difference and subtractNumbers
- Using arrow functions, repeat Steps 1-5 with new functions named multiply and multiplyNumbers and HTML form controls with IDs of factor1, factor2, product and multiplyNumbers
- Using any of the three function declaration types, repeat Steps 1-5 with new functions named divide and divideNumbers and HTML form controls with IDs of dividend, divisor, quotient and divideNumbers
- Test all of the mathematical functionality of the task3.html page.
Built-In Methods
Complete the following steps
In the task3.js file:
- Declare and instantiate a variable of type Date to hold the current date
- Declare a variable to hold the current year
- Using the variable declared in Step 1, call the built-in getFullYear() method/function and assign it to the variable declared in Step 2
- Assign the current year variable to an HTML form element with an ID of year
Array Methods
Complete the following steps
In the task3.js file:
- Declare and instantiate an array variable to hold the numbers 1 through 25
- Assign the value of the array variable to the HTML element with an ID of array
- Use the filter() array method to find all of the odd numbers of the array variable and assign the reult to the HTML element with an ID of odds ( hint: % (modulus operartor) )
- Use the filter() array method to find all of the even numbers of the array variable and assign the result to the HTML element with an ID of evens
- Use the reduce() array method to sum the array variable elements and assign the result to the HTML element with an ID of sumOfArray
- Use the map() array method to multiple each element in the array variable by 2 and assign the result to the HTML element with an ID of multiplied
- Use the map() and reduce() array methods to sum the array elements after multiplying each element by two. Assign the result to the HTML element with an ID of sumOfMultiplied
Review
After completing the previous steps:
- Review one of the many possible solutions for this task,
- Compare and contrast your code with the possible solution's code, and
- Make note of any improvements you could make for future tasks.