Except where otherwise noted, the contents of this document are Copyright © Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
The following links may help you in the debugging process:
alert()
and console.log()
to print debug messages.
"use strict";
at top of script file to enable strict syntax checking.
The following pages have mistakes that cause their JavaScript code not to work properly. Look at the pages, find the bug(s), and correct the problems.
onclick
attribute. Discover by using W3C HTML Validator.
buggy3: many syntax errors:
''
instead of "
to close string)
instead of }
to close functionDiscover by looking at Firebug error console, and/or by running JSLint.
}
brace. Discover by running JSLint (error message more helpful than Firebug's).
Using the browser's Javascript console, modify font.html
by changing the following in the paragraph #text
:
Once you have achieved these modifications, add your code to the page so that clicking the "Prettify" button will apply them. A runnable solution is located here (Don't peek at the code!).
Now, make it so that the JavaScript code is unobtrusive - ie there is no onclick handler in the HTML file
<div>
<button id="prettify" onclick="prettify();">Prettify</button>
</div>
function prettify() {
var text = document.getElementById("text");
text.innerHTML = "Look! It's pretty!";
// font styles added by JS:
text.style.fontSize = "13pt";
text.style.fontFamily = "Comic Sans MS";
text.style.color = "red";
}
Modify redblue.html
so that when the button is clicked, it randomly decides whether the background color of the page should be red, or if
the background color of the page should be blue. Hint: You can use document.body
to access the page's body in JavaScript.
A runnable solution is located here. (Don't peek at the solution code!)
Once you've gotten that working, make your JavaScript code unobtrusive (and way cooler) by taking out the button on the page and choosing a color randomly when the page loads.
<button onclick="chooseColor();">RedBlue?!</button>
function chooseColor() {
var choice = Math.round(Math.random());
if(choice == 1) {
document.body.style.backgroundColor = "blue";
} else {
document.body.style.backgroundColor = "red";
}
}
Create the file conversions.js referenced by conversions.html.
Write a function convert()
in conversions.js
that takes the value in the text input and converts it from either from kilograms to pounds or from pounds to killograms depending on the dropdown box.
The result should be displayed in the empty span with the id of #answer
.
The conversion factor from pounds to kilograms is 0.45359237, and the conversion factor from kilograms to pounds is 2.20462262.
You should edit the HTML file conversions.html to add ids to the elements as necessary and you may assume valid input. A runnable version of the solution is here. (Don't peek at the solution code!)
Solution conversions.html, and conversions.js:
function convert() {
var input = document.getElementById("input");
value = parseInt(input.value);
var conversion = document.getElementById("convert");
if (conversion.value == "kgtopounds") {
value *= 2.20462262;
} else {
value *= 0.45359237;
}
var answer = document.getElementById("answer");
answer.innerHTML = value;
}
Write a page that lets us perform various manipulations on the text in a text area. Here is the page (click the image to run our sample solution, written by TAs Sylvia Tashev and Stefanie Hatcher):
The HTML and CSS are already written, but we must add JavaScript code to make the UI respond when the user clicks the buttons. Start from the skeleton orderit.html.
Add the following behavior to the buttons:
document.getElementById
function.
split
method to break it into an array of smaller strings.
length
field and methods such as:
concat
,
join
,
pop
,
push
,
reverse
,
shift
,
slice
,
sort
,
splice
, and
unshift
.
More Behavior to add:
shuffle
method for arrays, but you can
write one using the following algorithm:
for each index i:
randomly choose an index j >= i.
swap lines i and j.
If you finish all of that behavior, consider adding a button to change the capitalization of lines, such as capitalizing/lowercasing entire lines or converting lines to AlTeRnAtInG cAsE.