patternjavascriptMinor
Personal questionnaire using JavaScript prompts
Viewed 0 times
questionnairejavascriptusingpromptspersonal
Problem
This is just playing around and practicing javascript. I am learning JavaScript from Codecademy and I practice coding everyday so I can learn much as possible. I have a lot of
```
function prompter() {
var toc = prompt("What do you prefer? Tea or Coffee");
if ((toc == "tea") || (toc == "Tea")) {
} else if ((toc == "coffee") || (toc == "Coffee")) {
}
var foc = prompt("What do you prefer? Facebook or MySpace");
if ((foc == "facebook") || (foc == "Facebook")) {
} else if ((foc == "myspace") || (foc == "Myspace")) {
}
document.write("So you like " + toc + " and " + foc + " that is awesome.");
}
function pickAColour() {
var fun = prompt("What is your favorite colour? , mine is red.");
document.write("You like the colour " + fun + " that is a nice colour");
}
function pickANumber() {
alert("Lets do some math, do you like Math?");
var pan = prompt("Please pick a number");
var nap = prompt("Please pick another number");
var symbol = prompt("Please pick one of the following symbols + - * /");
pan = parseInt(pan); //The parseInt() function parses a string and returns an integer.
nap = parseInt(nap); //The parseInt() function parses a string and returns an integer.
if (symbol == "+") {
alert("You picked the Add symbol");
alert("Lets add both these numbers together. " + pan + " " + symbol + " " + nap);
document.write("Your number is " + (pan + nap) + "");
}
if (symbol == "-") {
alert("You picked the Minus symbol");
alert("Lets Minus both of these numbers. " + pan + " " + symbol + " " + nap);
document.write("Your number is " + (pan - nap) + "");
}
if (symbol == "*") {
alert("You picked the Multiply symbol");
alert("
if statements, prompt boxes, some alert boxes. I just got finished learning about functions and returns. I just don't know how to use the return in a browser for like document.write or even an alert box.```
function prompter() {
var toc = prompt("What do you prefer? Tea or Coffee");
if ((toc == "tea") || (toc == "Tea")) {
} else if ((toc == "coffee") || (toc == "Coffee")) {
}
var foc = prompt("What do you prefer? Facebook or MySpace");
if ((foc == "facebook") || (foc == "Facebook")) {
} else if ((foc == "myspace") || (foc == "Myspace")) {
}
document.write("So you like " + toc + " and " + foc + " that is awesome.");
}
function pickAColour() {
var fun = prompt("What is your favorite colour? , mine is red.");
document.write("You like the colour " + fun + " that is a nice colour");
}
function pickANumber() {
alert("Lets do some math, do you like Math?");
var pan = prompt("Please pick a number");
var nap = prompt("Please pick another number");
var symbol = prompt("Please pick one of the following symbols + - * /");
pan = parseInt(pan); //The parseInt() function parses a string and returns an integer.
nap = parseInt(nap); //The parseInt() function parses a string and returns an integer.
if (symbol == "+") {
alert("You picked the Add symbol");
alert("Lets add both these numbers together. " + pan + " " + symbol + " " + nap);
document.write("Your number is " + (pan + nap) + "");
}
if (symbol == "-") {
alert("You picked the Minus symbol");
alert("Lets Minus both of these numbers. " + pan + " " + symbol + " " + nap);
document.write("Your number is " + (pan - nap) + "");
}
if (symbol == "*") {
alert("You picked the Multiply symbol");
alert("
Solution
You seem to be on the right track, at least with grasping how to do some basic coding.
Right now, I only see one major issue:
To do this, you will want to replace
You have the following HTML block:
Now, to insert text into this block, we would do something like this:
There's many things you can do with elements once you have gotten them, but I'll leave that for a later experiment.
At this point, I'll leave you to learn how to update your code. If you get confused or stuck however, you can view my rendition of your code here:
http://jsfiddle.net/danthegoodman/v7gLE/
Right now, I only see one major issue:
document.write()document.write() is used to build pages out of javascript, something that is both impractical and ill-advised (in almost every situation). Moving forward, you should start building your pages in HTML and use javascript to tweak the page from that point.To do this, you will want to replace
document.write() with document.getElementById(). Lets look at an example:You have the following HTML block:
Now, to insert text into this block, we would do something like this:
var welcomeElement = document.getElementById('welcome');
welcomeElement.innerHTML = "This is my welcome message!";There's many things you can do with elements once you have gotten them, but I'll leave that for a later experiment.
At this point, I'll leave you to learn how to update your code. If you get confused or stuck however, you can view my rendition of your code here:
http://jsfiddle.net/danthegoodman/v7gLE/
Code Snippets
<h1 id='welcome'></h1>var welcomeElement = document.getElementById('welcome');
welcomeElement.innerHTML = "This is my welcome message!";Context
StackExchange Code Review Q#13791, answer score: 7
Revisions (0)
No revisions yet.