Lab 18 Reflection

Once the HTML was set up I opened up the <script></script> tags and worked on the JS. I started out by declaring 3 global variables that would be used by the functions I would create later on: var x, which I would use to store the first number; var y, which I would use to store the second number; and var result, which I would use to store the result of the transformation done using the 2 numbers. I then started work on function add(), the function that would be called by the add button. I defined the variables but had not given them any value yet. When the button gets clicked I want the values put into the input boxes to be retrieved and stored into its respective variable. For this I used DOM. Inside the function I wrote the name of the variable gets document.getElementById(id_of_corresponding_textbox).value. So when I did this for my x variable it looked something like x = document.getElementById("num1").value. Once x and y got their corresponding variable I needed to perform the arithmetic function related to the button on the two values and store them in the result variable. For my add() function this was done by writing result = +x + +y. Finally, I needed to display the result in the webpage by changing the text between the <span> tags I set up earlier. This was done again using DOM with the line document.getElementById("result") = result. This concluded my add() function but I needed to make functions for all the other arithmetic functions. I used the same structure as my add() function for those but all I changed was the arithmetic function between x and y, so instead of addition it would be multiplication for the multiply() function. Once all my functions were defined I went back to the HTML and for the onClicks of each button I typed in the name of the corresponding function.
These skills are important for a web designer because they are essential in making a website interactive by changing elements of a webpage based on user interaction. A site can have buttons to click and areas to input text but having certain text change or images pop out when the user interacts adds to the experience. If you're running a site that sells goods, having the text change when the user makes a transaction adds to the user experience by giving confirmation to the user what they intended to do has happened. The DOM skills practiced in this lab help us replicate this effect by retrieving certain information in the webpage and programming that information to change based on what the user does.
Comments
Post a Comment