Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html><head>
- <meta charset="utf-8">
- <title>LIS 488: Lab 8</title>
- </head>
- <body>
- <h1>Part 1: Why is my code broken?</h1>
- <p>Help! I wrote this code, but it doesn't work! This part is supposed to figure out if someone is
- old enough to drink based on their birth year. By subtracting their birth year from the current year,
- we can get a number that's less than 21, in which case they can't drink; greater than 21, in which
- case they can; or equal to 21, in which case they might be able to: we'd have to get more information.
- <br>
- But my if statment doesn't work! Help meeeeee~~~!
- </p>
- <script>
- var birthYear = 1999;
- drinkingAge(birthYear);
- birthYear = 1996;
- drinkingAge(birthYear);
- birthYear = 1949;
- drinkingAge(birthYear);
- function drinkingAge(bYear){
- var currentYear = 2017;
- document.write("<p align=center>You were born in: " + bYear + ". Therefore, you ");
- var age = currentYear - bYear;
- if(age = 21){
- document.write ("<strong>might</strong> be old enough to drink. I'm going to need to see some ID.</p>");
- }
- else if(age < 21){
- document.write("are <strong>not</strong> old enough to drink, sorry!</p>");
- }
- else{
- document.write("are old enough to have a drink. Cheers! (Please drink responsibly)</p>");
- }
- }
- </script>
- <hr width="50%" align="center">
- <p>It's just not a good day for me and code. This while loop is also broken.
- I wanted it to print the numbers between 1 and 10, but it won't work!</p>
- <script>
- var i = 0;
- while(i<10){
- document.write("<p>"+i+"</p>");
- i++;
- }
- </script>
- <hr width="50%" align="center">
- <p>Now this stupid form won't work either! Programming stinks! (Just kidding! I love you, computers!)
- I wanted this to greet the user and give them an random ID number when they click the button, but nothing happens!
- </p>
- <form>
- Name: <input type="text" name="user">
- <br>
- <br>
- <input name="greet" value="Click me!" type=button onClick=makeId(this.form)>
- </form>
- <p id="IDNum">
- </p>
- <script>
- function makeID(f){
- var name = f.user.value;
- if (name == ""){
- document.getElementById("IDNum").innerHTML=("Please enter your name");
- }
- else{
- var id=Math.floor((Math.random() * 10) + 1);
- //http://www.w3schools.com/js/js_switch.asp
- var house;
- switch(id%4){
- case 0:
- house = "Hufflepuff";
- break;
- case 1:
- house = "Slytherin";
- break;
- case 2:
- house = "Gryffindor";
- break;
- default:
- house = "Ravenclaw (the best house)";
- }
- document.getElementById("IDNum").innerHTML=(name +", your ID number is " + id +", which puts you in House " + house);
- }
- }
- </script>
- <p>
- Actually, there's quite a bit going on in that last script.
- Write a brief description of what the code is doing when the makeID function is called:
- <!-- your description goes here -->
- </p>
- <h1>Interlude: a working example</h1>
- <p>
- The form and code below are from the slides: they show how to get a number from a form, convert it to another number, and then
- report back the new number. In this case, we are converting from human years to dog years. For the rest of the assignment,
- the goal is for you to use this form and script as a template, and modify the names of the variables and IDs so that they
- are appropirate for the equation you are being asked to do.
- </p>
- <p>
- </p><form>
- Your age:<br>
- <input type="text" id="age">
- <input value="Calculate" type="button" onclick="dogYears()">
- <p id="dogAge">
- </p>
- </form>
- <p>
- <script>
- function dogYears(){
- var age = parseFloat(document.getElementById("age").value);
- var da = age/7;
- document.getElementById("dogAge").innerHTML=("You are " + da + " years old in dog years!");
- }
- </script>
- </p><hr width="50%" align="center">
- <h1>Part 2: Is it colder in Europe?</h1>
- <!--
- 1. In the HTML, make a form that contains the following:
- - a text input named DegF
- - a button input named calc, with the onClick set to “FahToCel”
- - a paragraph with the id “DegC”
- 2. After the form, create <script> tags and create a function called “FahToCel”.
- 3. Create a variable called “fah” that finds the value of the DegF input from the form and converts it to a number.
- 4. Convert fah to Celsius using this equation:
- var cel = (fah - 32.0) * 5.0 / 9.0;
- 5. Display the value of the cel variable in the paragraph with the DecC id
- -->
- <hr width="50%" align="center">
- <h1>Part 3: Am I taller in Canada??</h1>
- <!--
- Based on the steps in Part 2, create another form and script combination.
- This one should take in a height in inches and convert it to centimeters (cm =
- inches * 2.54) -->
- <hr width="50%" align="center">
- <h1>Part 4: Dealer's choice</h1>
- <!--
- Write your own code! This one can do whatever you want: it can use a form, or just call a function, whatever! A few ideas:
- - Ask user for their name and greet them
- - Ask for a weight in lbs and convert it to kgs
- - Ask the user a question and display something different depending on their answer:
- - Have the user guess a number that you picked, and tell them if they got it right or wrong
- - Ask their favorite color and comment on it (oooh, or change the background of the page to match it! You'll need to do a bit of extra research for that, though)
- - Ask them a question and provide them with different buttons: depending on what button they push, do something different
- -->
- </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement