Extra Help on Section 2
In this lesson, we have met several functions, namely alert(), prompt(), parseInt(), parseFloat() and eval(). (Have I forgotten any? If so, I apologise.) One thing they all do have in common is that the name of the function is followed by a pair of brackets, like this (). There may things inside the brackets (called parameters, you will remember), or the brackets may be empty, but, empty or not, the brackets must be there! If you didn't put them in, then JavaScript would have no way of telling whether the thing you were referring to was a function or a simple variable. In fact, you might like to try using a simple variable called prompt (no brackets in that case), just to see if JavaScript will let you do it. However, there are differences between them. Two functions that you have met, alert() and document.write(), are used on their own and not in conjunction with an = sign. You would use alert(), for instance, like this: alert("Listen to this, folks!"); and not like this: some_variable = alert("Listen to this, folks!"); We say that alert() does not "return a value." All the other functions that you have met so far, do return values. They "give back" numbers or strings which should be used somehow - either assigned to variables or displayed on the screen (using document.write() or alert()). For instance, parseInt() would be used like this: numerical = parseInt("1234"); or like this: document.write(parseInt("1234")); but not like this: parseInt("1234"); You will get to know which functions are used which way as you come across them. In most cases it will be fairly obvious which way the function should be used.
When you include the statement prompt in a program you must remember to put a pair of brackets after it, and these brackets must contain two strings with a comma between them. You must also set the value that prompt produces to something, in the same way that you would set a value to a variable. Here a few examples of prompt statements. The first one is written correctly, but all the others have something wrong with them, and we will dissect them all in turn.
my_var = prompt("Please enter something", "xxx");
Let's check this against our list of requirements:
Now come the series of defective examples, starting with
var temp1 = prompt("Enter the response" "first" "second");
There are two things wrong with this. Firstly, the prompt requires two strings to be passed to it - no more and no fewer - and yet here it is being given three! Secondly, where is the comma to separate the strings? It's not there!
prompt "What is the capital of England?","London"
Again, this has two things wrong with it. There is no pair of brackets surrounding the strings and after the word prompt. Secondly, the instruction has just been put in without being assigned to a variable, or even displayed on the screen. The reason that this is wrong is that prompt produces a value, a string in fact, and that string has to go somewhere. It can't just disappear into the ether - it must be assigned to a variable (the most usual fate) or displayed on the screen, which is something else you can do with variable values. One more:
prompt("String 1", "String 2") = temp
This has two things wrong with it again. Firstly, there should be a semicolon (;) at the end of the instruction, as there should be at the end of most instructions in JavaScript (you may have noticed that the previous example was missing one as well). Secondly, and more importantly, the assignment can't be this way round. The prompt instruction is the one that produces the value, and the variable is the thing that accepts that value. If you put the prompt on the left of the = sign and the variable on the right, then it looks as though the variable is trying to pump a value into the prompt instruction. JavaScript wouldn't like that!
No, they can be variables instead. Here's what I mean. The following prompt instruction may look wrong, but it is in fact legal: var x = "Enter your age"; age = prompt(x, "26"); The reason it looks wrong is because the first thing inside the brackets doesn't have double quotation marks round it, so can't be a string. But it is a string! It's a string variable, i.e. a variable that holds a string value, in this case Enter your age. In this case, the second instruction has the same effect as if it had been the instruction: age = prompt("Enter your age", "26"); In fact, JavaScript is more tolerant of what counts as a string and what doesn't than you might think. Here is that same example with a slight change: var x = 36.11; age = prompt(x, "26"); Now, I agree with you. In this case, x is a numerical variable - it holds a number. However, JavaScript will happily create a string version of that number for us, just for this purpose. In this case, the instruction will work as if it had been written like this: age = prompt("36.11", "26"); which produces a rather unusual prompt, I admit. The same thing applies to the second string, the default value. This can also be a number or a numerical variable, and JavaScript will turn a blind eye, and interpret it as a string on this occasion. Please note, this doesn't mean that the variables are converted to string values. No, they stay as numerical variables, and can be treated as numbers (added, subtracted etc.) afterwards. It just means that JavaScript pretends that they hold strings just while the prompt command is being executed. After that, the temporary conversion is forgotten about entirely.
This is a similar comment to the one for prompt() except that only one string is required, so no commas are necessary. The string can be a constant value: alert("Program has ended."); or a string variable message = "Reached point X in the program"; document.write(message); or even a number (such as a constant, a numerical variable or the result of a calculation): var x = 20; alert(104.7); document.write(2 * x - 9.8); In the case of numbers or numerical variables or expressions, alert() and document.write() interpret the numbers as though they were strings and display them anyway - they're not too fussed that what they have been given is actually a number!
x = 2; // Set x to 2 Duh! We know! There is no point in this comment whatsoever. It simply states what we can see for ourselves by looking at the instruction. Try to make the comments provide more useful information: x = 2; // Player has 2 lives That's better! Now we can see exactly what the purpose of x is. Clearly this line has been extracted from some game program and x stores how many "lives" the player has before "game over". Of course, a better name for the variables would have been lives, but at least the comment now does something useful! |