Lesson 10 — Practical Example
A simple intro to JavaScript
Previous :: Next
numberGuess.html
Open up Brackets and create a new file in the JavaScript folder by creating a new file. ( ⌘ + N / Cmd + N) Then rename the file to numberGuess.html. Type in this code into the file and then save the file:
<script>
while (true) {
msg = "Pick a number between 1-100";
answer = prompt(msg);
if (answer == "") {
answer = confirm("Exit app? ");
if (answer) {
exit();
} else {
continue;
}
}
if (answer == 42) {
break;
}
alert("Try again");
}
</script>
As you can see, I didn’t comment anything on this program. This is because I want you to go through and try to solve it without any help at all!
tempConverter.html
Open up Brackets and create a new file in the JavaScript folder. ( ⌘ + N / Cmd + N) Then rename the file to tempConverter.html. Type in this code into the file and then save the file:
<script>
/* This program convers F to C
*/
alert("Welcome to my F to C Program");
function toC(f) { // Create function to convert
return (5/9) * (f - 32);
}
while (true) { // We use while "true" to continue until there is empty input
var f = prompt("Enter degrees F"); // Prompt to enter value to convert
if (f == "") {
var answer = confirm("Exit?");
if (answer == true) {
alert("Goodbye");
break;
}
continue;
}
var c = toC(f);
alert(f + " F = " + c + " C");
}
</script>
However, this time, the comments in my code explain the process through which the computer goes through and what my code means.
Previous :: Next