Okay I see your point. So you need a JavaScript book that isn't too easy, but sort of easy. Just enough to get the hang of it.
Anyways...
Easy.Quote:
what for-loop is, how it works, and give an example of how it's used
A for loop in JS is exactly the same as C++ (just isn't as strict). There's only one type of variable in JS, no int, double, etc. So. Here's a basic for-loop
For-loops can be useful in verifying information, running through a sequence of arrays etc.Code:for (var i = 0;i < 25;i++) {
document.write(i)
//display the value of "i"
}
An array is a variable that stores data in sequence:
So you could say...Code:var myArray = ["value 0","value 1","value 2","value 3","this is another value"]
Which would display "value 1"Code:document.write(myArray[1])
Not that hard when you look at it from a glance.
So if you can store data using a decimal value, here's where your for-loop comes in.
Let's take a look at this code:
The "entries" variable are all the possibilities. The for-loop runs numbers from 0 to the array "entries" length (entries.length is how many values there are in entries). If a winner is found in the input (the argument "contestant") it will alert saying the winners' name. Not too complex, just a little experiment with the for-loop. :)Code:<html>
<head>
<script type="text/javascript">
function results(contestant) {
var entries = ["jonathan","damien","courtney","kyle"]
for (var i = 0;i < entries.length;i++) {
if (contestant.value == entries[i]) {
alert(entries[i]);
}
}
}
</script>
</head>
<body>
Type in the winners name:
<br><input type="text" id="winner">
<br><input type="button" value="Go" onclick="results(winner)">
</body>
</html>
