Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know there is a a lot of confusion in my question, and I am a beginner in javascript, so please bear with me. I am looping through an array of 120 numbers using java script and I am trying to print out certain words under certain conditions. When a number in the array is divisible by 3 I want to print Go, when its divisible by 5, I want to print GoGo and when its divisible by both 3 and 5 and I to print out GOGOGadget.

I am nested an if statement inside my for loop and I also want to append the printout to the DOM.

html:

<html>
</head>
  <body>
    <h1>Javascript printout</h1>



  <button>
    Click Me
  </button>
  <p class="printout">0</p>

  </body>
</html>

JavaScript :

( document ).ready(function() {
   $("button").click(function(){
     var Count = ["1", "2", "3","4"];  
     for (var i = 0; i > 4; i++) {
       if (Count%1) {
         $("printout").text("Go");
         $("printout").append(Go);
       }
     }
   });
});
share|improve this question
    
$("printout") is looking for a DOM tag named "printout" -- like "div" or "a". What is the HTML you are using? –  Casey Falk Aug 13 '14 at 20:19
3  
All numbers are divisible by 1. –  elclanrs Aug 13 '14 at 20:19
    
Also, what is the specific error you're asking about? :) –  Casey Falk Aug 13 '14 at 20:20
1  
That will never enter the for loop :o! –  SpartanElite Aug 13 '14 at 20:21
3  
-10 for the worst question I have ever seen here. +9 for making me laugh. –  gang Aug 13 '14 at 20:23

3 Answers 3

Your condition is incorrect.

anything%1 will return 0, which in JS is falsy. So in this case, that branch will never run. What you really want is Count[i]%1==0

share|improve this answer

I see a few problems/points of confusion here.

1) All integers are divisible by one, and your set of inputs show only integers, so it's not clear to me, when you want to distinguish non-integers from integers, or if there is a misunderstanding here.

2) Your condition is the opposite of what you're trying to check for, i.e. 1%1 === 0, so if Count is 1, the condition will fail (0 is 'falsy').

3) You never actually check for even numbers, i.e. divisible by 2.

In this example, you'd never print anything because your inputs are all "divisible by 1", at least by the semantics of the javascript % operator.

share|improve this answer
  1. Count is unnecessary, since you have i which starts as 0 and increases by 1 with each iteration.
  2. $("printout") is looking for a DOM element like this -> <printout></printout>. What you probably want is either $("#printout") or $(".printout") to find an ID or Class respectively.
  3. Don't use append, just change the text for each condition.
  4. (Count % 1) evaluates to 0. Which is a falsey value in javascript. What you want is ((Count % 1) === 0) because 0 === 0 is a truthy value. Remember though, don't use Count, just use i.
  5. All integers are divisible by 1. Make your first condition be i === 1.
  6. Change the condition for the for loop from i > 4 to i < 4. Otherwise it wont even enter into the loop.
  7. What are you even trying to do? this question is nearly incomprehensible.

for (var i = 0; i < 4; i++) {

  if (i === 1) {
    $("printout").text("Go");
  } else if ((i % 2) === 0) {
    $("printout").text("GoGo");
  } else if ((i % 3) === 0) {
    $("printout").text("GoGoGadget");
  }
}
share|improve this answer
1  
Also, as the condition of the for loop says i > 4, there will be zero iterations. –  gang Aug 13 '14 at 20:36
    
Thanks for catching that. There were so many errors, it's hard to spot them all haha. If anyone see's anything else, feel free to edit the answer. –  emilySmitley Aug 13 '14 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.