submissions:worksheet:webdev:web_dev_week8
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | submissions:worksheet:webdev:web_dev_week8 [2020/06/20 14:39] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~CLOSETOC~~ | ||
+ | |<100% 25% - >| | ||
+ | ^ \\ DATA ANALYTICS REFERENCE DOCUMENT\\ \\ ^^ | ||
+ | ^ Document Title:|Web Development week 8 assignments| | ||
+ | ^ Document No.: | ||
+ | ^ Author(s): | ||
+ | ^ Contributor(s): | ||
+ | |||
+ | |||
+ | **REVISION HISTORY** | ||
+ | |< 100% 10% - - 10% 17% 10% >| | ||
+ | ^ \\ Revision\\ | ||
+ | | [[: | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== Web Development - Week 8 ====== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== Exercise 1 ===== | ||
+ | <code javascript DOM_manipulation.html> | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <button onclick=" | ||
+ | <H1 id=" | ||
+ | </ | ||
+ | < | ||
+ | // gets input text when the button is clicked and updates the h1 | ||
+ | | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Exercise 2 - Basic Calculator ===== | ||
+ | |||
+ | Create a web page that has 2 input boxes and a button that adds up the numbers in the boxes. | ||
+ | |||
+ | <code javascript calculator.html> | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | <input id=" | ||
+ | <input id=" | ||
+ | <button onclick=" | ||
+ | |||
+ | </ | ||
+ | < | ||
+ | // gets input text when the button is clicked and updates the h1 | ||
+ | | ||
+ | v1=Number(document.getElementById(" | ||
+ | v2=Number(document.getElementById(" | ||
+ | alert(v1+v2); | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Exercise 3 - Advanced calculator ===== | ||
+ | |||
+ | {{ submissions: | ||
+ | |||
+ | <code javascript advanced_calc.html> | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <p> | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <p id=' | ||
+ | </ | ||
+ | < | ||
+ | function procVals(ttype){ | ||
+ | |||
+ | // gets input text when the button is clicked and updates the h1 | ||
+ | var v1=Number(document.getElementById(" | ||
+ | var v2=Number(document.getElementById(" | ||
+ | //var ans=Number(0); | ||
+ | |||
+ | switch(ttype){ | ||
+ | case 0: | ||
+ | ans= v1 + " plus " + v2 + " = " + Number(v1+v2); | ||
+ | break; | ||
+ | case 1: | ||
+ | ans= v1 + " minus " + v2 + " = " + Number(v1-v2); | ||
+ | break; | ||
+ | case 2: | ||
+ | ans= v1 + " multiplied by " + v2 + " = " + Number(v1*v2); | ||
+ | break; | ||
+ | case 3: | ||
+ | ans= v1 + " devided by " + v2 + " = " + Number(v1/ | ||
+ | break; | ||
+ | } | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | Code provided by Mike. | ||
+ | |||
+ | <code javascript advanced_calc_2.html> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | < | ||
+ | |||
+ | <!-- Inputs for numbers --> | ||
+ | < | ||
+ | < | ||
+ | |||
+ | <!-- Buttons for each operator --> | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | |||
+ | <!-- Output for answers --> | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | // | ||
+ | function addNum(){ | ||
+ | //Make a var called num1 equals to the value inside the input box with an id of " | ||
+ | var num1 = document.getElementById(" | ||
+ | //Make a var called num1 equals to the value inside the input box with an id of " | ||
+ | var num2 = document.getElementById(" | ||
+ | // | ||
+ | // | ||
+ | var total = Number(num1) + Number(num2); | ||
+ | //Output the result to the " | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | |||
+ | // | ||
+ | function subNum(){ | ||
+ | var num1 = document.getElementById(" | ||
+ | var num2 = document.getElementById(" | ||
+ | var total = Number(num1) - Number(num2); | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | |||
+ | //Function to multiply contents of input box 1 and input box 2. Almost identical as first function but multiplies instead | ||
+ | function mulNum(){ | ||
+ | var num1 = document.getElementById(" | ||
+ | var num2 = document.getElementById(" | ||
+ | var total = Number(num1) * Number(num2); | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | |||
+ | // | ||
+ | function divNum(){ | ||
+ | var num1 = document.getElementById(" | ||
+ | var num2 = document.getElementById(" | ||
+ | var total = Number(num1) / Number(num2); | ||
+ | document.getElementById(" | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Exercise 4 - Guessing Game 1 to 10 ===== | ||
+ | |||
+ | Write a JavaScript program where the program generates a random number between 1 to 10 . The user is then prompted to input a number. If the number the user input matches with the random number, the program will display a message "Good Guess", | ||
+ | |||
+ | How to generate a random number between 1 and 10 in JavaScript: | ||
+ | |||
+ | <code javascript> | ||
+ | var num = Math.ceil(Math.random() * 10); | ||
+ | </ | ||
+ | |||
+ | <code javascript> | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | <body onload=" | ||
+ | <p id=" | ||
+ | < | ||
+ | <button onclick=" | ||
+ | <p id=" | ||
+ | |||
+ | </ | ||
+ | < | ||
+ | var secretVal=0; | ||
+ | function addVals(){ | ||
+ | vGuess=Number(document.getElementById(" | ||
+ | if (vGuess==secretVal) | ||
+ | { | ||
+ | document.getElementById(' | ||
+ | } | ||
+ | else { | ||
+ | document.getElementById(' | ||
+ | } | ||
+ | |||
+ | } | ||
+ | function runAtStart(){ | ||
+ | secretVal = Math.ceil(Math.random() * 10); | ||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | ===== Exercise 5 - Guessing Game 1 to 100 ===== | ||
+ | |||
+ | {{ submissions: | ||
+ | |||
+ | Write a JavaScript Guessing Game program where the program chooses a random number between 1 to 100. The user is then prompted to guess the number the program chose. The user has six attempts to guess the right number. After each attempt, if the number is incorrect, the program will display “Guess too high” or “Guess too low” depending on the user input. If the user has not guessed the correct number after 6 attempts, the game ends and the number the program generated is displayed. | ||
+ | |||
+ | <code javascript guess_100.html [enable_line_numbers=" | ||
+ | < | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | </ | ||
+ | <body onload=" | ||
+ | <p id=" | ||
+ | <p id=" | ||
+ | < | ||
+ | <button onclick=" | ||
+ | <p id=" | ||
+ | |||
+ | </ | ||
+ | < | ||
+ | var secretVal=0; | ||
+ | var tries=0; | ||
+ | var correct =0; | ||
+ | function addVals(){ | ||
+ | vGuess=Number(document.getElementById(" | ||
+ | tries+=1; | ||
+ | document.getElementById(' | ||
+ | if (vGuess< | ||
+ | { | ||
+ | document.getElementById(' | ||
+ | } | ||
+ | if (vGuess> | ||
+ | document.getElementById(' | ||
+ | } | ||
+ | if (vGuess==secretVal) | ||
+ | { | ||
+ | document.getElementById(' | ||
+ | correct=1; | ||
+ | runAtStart(); | ||
+ | } | ||
+ | if (tries> | ||
+ | { | ||
+ | document.getElementById(' | ||
+ | runAtStart(); | ||
+ | } | ||
+ | } | ||
+ | function runAtStart(){ | ||
+ | secretVal = Math.ceil(Math.random() * 100); | ||
+ | tries=0; | ||
+ | correct=0; | ||
+ | document.getElementById(' | ||
+ | document.getElementById(' | ||
+ | document.getElementById(' | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Code provided by Mike | ||
+ | |||
+ | <code javascript guess_100_v2.html> | ||
+ | < | ||
+ | < | ||
+ | <!-- Week 7 Exercise - Guessing Game | ||
+ | Module: WEB APPLICATION DEVELOPMENT | ||
+ | --> | ||
+ | |||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | /* generation of the random variable number guessNumber | ||
+ | the method Math.random() generates a number between 0 and 1, excluding 0 and 1 | ||
+ | the method Math.ceil() rounds the number for the upwards nearest integer | ||
+ | therefore, the number generated will be between 1 (inclusive) and 100 (inclusive)*/ | ||
+ | var guessNumber = Math.ceil(Math.random() * 100); | ||
+ | |||
+ | //Alert created below for testing purposes, this can be removed later | ||
+ | alert(guessNumber); | ||
+ | |||
+ | |||
+ | //for loop - this loop will run six times, and creates a counter called " | ||
+ | //" | ||
+ | for (var i = 1; i <= 6; i++){ | ||
+ | |||
+ | // prompt the user to enter a number | ||
+ | var userNumber = Number (prompt(" | ||
+ | |||
+ | // compare the number entered with the one generated | ||
+ | if (userNumber == guessNumber){ | ||
+ | // If the number entered was correct, the using " | ||
+ | break; | ||
+ | } else {// If the number entered was not correct | ||
+ | // | ||
+ | if (userNumber > guessNumber){ | ||
+ | //If the number guessed was higher, output message stating it's too high | ||
+ | //The code calculation (6-i) will calculate how many turns are left | ||
+ | alert(" | ||
+ | } else { //If the number guessed was lower, output guess was too low | ||
+ | alert(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | //Final test after the six attempts are finished, or if the user guessed the correct number before the six attemps were up | ||
+ | if (userNumber == guessNumber){ | ||
+ | alert(" | ||
+ | } else { | ||
+ | alert(" | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ |
submissions/worksheet/webdev/web_dev_week8.txt · Last modified: 2020/06/20 14:39 by 127.0.0.1