0

I have an array that i wish to looking into and only print something out when the array== another variable, however it seems to print out the statement whatever, am I missing something stupid?

var candArray = 
['Green...', 'Brown...', 'Black...', 'White...', 'Grey....','Blue....', 'Pink....'];

var votesArray= [33,51,43,22,61,51,47];
var maximumVote = 61;

for(i=0; i<candArray .length; i++){
if(votesArray[i] == maximumVote);{
 document.write(candArray [i] + ' is hearby declared elected');}
}

All help appreciated. Thanks

EDIT: OMG i knew it was something simple, but ive been doing it so like 2 hours and just got into that phase where you dont check the simple things because you're too wound up. many thanks all

2
  • Is there also a space between candArray and .length in your actual code, or is that only from what you posted here? Commented Jun 16, 2011 at 16:59
  • 4
    You should really use var i so your loop variable doesn't become global. Commented Jun 16, 2011 at 17:00

5 Answers 5

5

Remove the ; after the if.

An if statement affects the statement or block immediately after it.

You have an empty statement (;) after your if, so the subsequent block is not affected by the if.

Sign up to request clarification or add additional context in comments.

Comments

2

You had a semi-colon after your if condition... It nullified your conditional, and then the code block that followed ran every time.

for(i=0; i < candArray.length; i++){
 if(votesArray[i] == maximumVote) {
   document.write(candArray [i] + ' is hearby declared elected');
 }
}

Comments

2

Remove the ; after the if statement:

var candArray = 
['Green...', 'Brown...', 'Black...', 'White...', 'Grey....','Blue....', 'Pink....'];

var votesArray= [33,51,43,22,61,51,47];
var maximumVote = 61;

for(i=0; i<candArray .length; i++){
if(votesArray[i] == maximumVote){ //<--remove the ;
 document.write(candArray [i] + ' is hearby declared elected');}
}

Comments

2

You have a semicolon after your 'if' statement that's messing it up.

Fixed:

var candArray = 
['Green...', 'Brown...', 'Black...', 'White...', 'Grey....','Blue....', 'Pink....'];

var votesArray= [33,51,43,22,61,51,47];
var maximumVote = 61;

for(i=0; i<candArray .length; i++){
if(votesArray[i] == maximumVote){
 document.write(candArray [i] + ' is hearby declared elected');}
}

Comments

1

The problem is a misplaced semicolon.

if(votesArray[i] == maximumVote);{
  document.write(candArray [i] + ' is hearby declared elected');}
}

should be:

if(votesArray[i] == maximumVote) {
  document.write(candArray [i] + ' is hearby declared elected');}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.