Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble with FOR LOOP statement!

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with FOR LOOP statement!

    Hi

    I'm a code cadet and I'm having trouble with this exercise:

    "We've already declared a variable for you in the editor called number to which the value 42 was assigned.
    Use a for loop to go through all numbers from number up to 50 (both inclusive), and check if they are multiples of 3. If they are, print them."

    My code is:

    var number = 42
    for (var number = 42; number <= 50; number++) {
    if (number % 3 === 0)
    console.log(number);
    }

    And the result is >>>>Code is incorrect
    You should have an if statement to check if each value of the variable number is a multiple of 3

    I can't find what I'm doing wrong, can you please help me? Also the answer that is giving me it's '51' and that's not a multiple o 3.

    Thanks!
    Last edited by Joantunes7; November 15th, 2021 at 10:09 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with FOR LOOP statement!

    There are several syntax errors in the posted code. Look at the compiler's error messages and fix the errors.
    If you need help fixing the errors, post the full text of the error message and ask any questions you have about fixing it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with FOR LOOP statement!

    Already solved! Thanks for your help! The code was correct but the test program was bugging with something that I still don't know what it was. I deleted everything and did it again exactly in the same way and it was correct.

    The syntax errors were already edited in this post but they were exclusively on this post because I couldn't copy and paste from the original test program and I didn't noticed those typing errors here! Sorry about that

    Cheers

Similar Threads

  1. Replies: 2
    Last Post: December 10th, 2019, 06:17 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Replies: 3
    Last Post: January 25th, 2013, 06:53 AM
  4. Having trouble with for loop statement.
    By Truck35 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 25th, 2012, 04:45 PM
  5. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM