Why does document.write not work?
Why does the document.write method not work?
Here's my code:
<SCRIPT LANGUAGE = "JavaScript">
var candidateArray = ['Mr R Green...', 'Ms O Brown...', 'Ms Y Black...', 'Mr G White...', 'Ms B Grey....','Ms I Blue....', 'Mr V Pink....'];
var onlineVotesArray = [21,47,23,11,56,47,30];
var paperVotesArray = [12,4,20,11,5,4,17];
var totalVotesArray;
var x = 0;
var lengthPaperVotesArray = length(paperVotesArray);
for x in range(lengthCandidateArray) {
totalVotes = onlineVotesArray(x) + paperVotesArray(x);
totalVotesArray(x) = totalVotes;
x = x开发者_运维百科 + 1;
}
document.write(totalVotesArray);
</SCRIPT>
By the way, I have to use the document.write method and no other variant.
Your syntax is all wrong.
To access item in an array, use []
not ()
, and for/in
loop requires parentheses around the expression.
//-v--------------------------------v------parentheses
for (x in range(lengthCandidateArray)) {
totalVotes = onlineVotesArray[x] + paperVotesArray[x];
// ---------------v-v-----------^-^------------------^-^-----square brackets
totalVotesArray[x] = totalVotes;
x = x + 1;
}
Additionally, you're calling length
and range
methods. If you don't have them in another script, then they don't exist.
If you simply meant to get the .length
of the array, use the property. And you don't need a range
method to loop.
var candidateArray = ['Mr R Green...', 'Ms O Brown...', 'Ms Y Black...', 'Mr G White...', 'Ms B Grey....','Ms I Blue....', 'Mr V Pink....'];
var onlineVotesArray = [21,47,23,11,56,47,30];
var paperVotesArray = [12,4,20,11,5,4,17];
var totalVotesArray = [];
var lengthPaperVotesArray = paperVotesArray.length;
for (var x = 0; x < lengthPaperVotesArray; x++) {
totalVotesArray[x] = onlineVotesArray[x] + paperVotesArray[x];
}
document.write(totalVotesArray);
Example: http://jsfiddle.net/8fkZq/
EDIT::
If you wanted your output to incorporate the candidates like this:
Mr R Green... : 33,
Ms O Brown... : 51,
Ms Y Black... : 43,
Mr G White... : 22,
Ms B Grey.... : 61,
Ms I Blue.... : 51,
Mr V Pink.... : 47
...you could change the loop:
for (var x = 0; x < lengthPaperVotesArray; x++) {
totalVotesArray[x] = '<br>' + candidateArray[x] + ' : ' + (onlineVotesArray[x] + paperVotesArray[x]);
}
Example: http://jsfiddle.net/8fkZq/1/
You're referencing "lengthCandidateArray" but this is not defined.
If you opened firebug, you probably would have seen a related error. This is critical information if you ever ask questions in the future.
It seems you have some error in your javascript code
var totalVotesArray = [];
var x = 0;
var lengthPaperVotesArray =paperVotesArray.length;
for (x = 0;x < lengthPaperVotesArray ; x++){
totalVotes = onlineVotesArray[x] + paperVotesArray[x];
totalVotesArray.push(x) = totalVotes;
}
document.write(totalVotesArray.join(', '));//i don't think you can write an array
This is riddled with syntax errors. I don't know what languages you're used to programming in, but jScript is clearly not one of them. First off, length(x) is not a global method. If you wanna get the length of an array, you use array.length
. Next the syntax for for...in in js is for (x in array) { ... }
. Also, if you want to reference a variable as an array, you have to declare it as such ( var array = [];
). Finally, to access the elements of an array, you use array[x]
, not array(x). Try this instead:
<SCRIPT type="text/javascript">
var candidateArray = ['Mr R Green...', 'Ms O Brown...', 'Ms Y Black...', 'Mr G White...', 'Ms B Grey....','Ms I Blue....', 'Mr V Pink....'];
var onlineVotesArray = [21,47,23,11,56,47,30];
var paperVotesArray = [12,4,20,11,5,4,17];
var totalVotesArray = [];
var x = 0;
var lengthPaperVotesArray = paperVotesArray.length;
for (x in paperVotesArray){
totalVotes = onlineVotesArray[x] + paperVotesArray[x];
totalVotesArray[x] = totalVotes;
x = x + 1;
}
document.write(totalVotesArray);
</SCRIPT>
精彩评论