maniuni
maniuni

Reputation: 77

Do I understand correctly the way logical AND works?

I have a simple homework assignment: Write a script that prints all the numbers from 1 to N, that are not divisible by 3 and 7 at the same time.

So, here is my solution. The way it works - it prints all numbers that divide by 3 OR by 7 instead of 3 AND 7. Please help me understand why is this not working as I need. Here is the code:

<!DOCTYPE html>
<html>
<head>
    <title>Loops - Task 2</title>
<link href="js-console.css" rel="stylesheet" />
</head>
<body>
    <label for="input_num">Start: </label>
    <input type="text" id="input_num" />

    <a href="#" id="btn" onclick="printNums()">Print numbers</a>

    <div id="js-console"></div>              
    <script src="js-console.js"></script>
    <script>
        var input_num;
        var counter;
        function printNums(){
            input_num = jsConsole.readInteger("#input_num");
            for (counter=1; counter<=input_num; counter++){
                if(counter % 3 !=0 && counter % 7 != 0){
                    jsConsole.writeLine(counter);
                }
              }
    }
    </script>
</body>
</html>

Upvotes: 3

Views: 140

Answers (5)

ObieMD5
ObieMD5

Reputation: 2657

In order to be divisible by both, using mod 21 will return 0. This shows in 21, 42,63,etc.

if(counter % 21 != 0){

EDIT

<!DOCTYPE html>
<html>
<head>
    <title>Loops - Task 2</title>
<link href="js-console.css" rel="stylesheet" />
</head>
<body>
    <label for="input_num">Start: </label>
    <input type="text" id="input_num" />

    <a href="#" id="btn" onclick="printNums()">Print numbers</a>

    <div id="js-console"></div>              
    <script src="js-console.js"></script>
    <script>
        var input_num;
        var counter;
        function printNums(){
            input_num = jsConsole.readInteger("#input_num");
            for (counter=1; counter<=input_num; counter++){
                if(counter % 21 != 0){
                    jsConsole.writeLine(counter);
                }
              }
    }
    </script>
</body>
</html>

The way mod (%) works is it will give you the remainder of the division. If you divide 4/2 = 2 evenly without any left over. However, 5/2 will return 2 still with 1 remainder . The 2 goes into 4, 2 times evenly with 1 left over hence: 4%2 = 0 5%2=1 Since you are saying that it cannot be divisable by 3 and 7 at the same time, you can muliply your two numbers together to use mod and the first number divisiable by 3 and 7 together is 21.

Upvotes: 2

Graham Fowles
Graham Fowles

Reputation: 449

if i understand the assignment correctly you want to print out numbers that will not divide by 3 and 7 but can divide by one or none of them. IE 3 and 7 will print out because they can be divided by one but not the other, but 21 will not print as it divides by both.

in which case the logic should be like:

if(!((counter % 3) == 0 && (counter % 7) == 0))

Upvotes: 1

Stasv
Stasv

Reputation: 115

if((counter % 3) !=0 && counter % 7 != 0)

should be :

if (!( (counter % 3) ==0 && counter % 7 ==0))

or just go with the 21 tip...

Upvotes: 0

thorn0
thorn0

Reputation: 10387

"Are not divisible by 3 and 7 at the same time" means

!(x % 3 == 0 && x % 7 == 0)

Or, if we apply De Morgan's law:

x % 3 != 0 || x % 7 != 0

Upvotes: 1

nikodz
nikodz

Reputation: 727

if(counter % 3 !=0 && counter % 7 != 0)

This means that if number can divide by 3 then counter%3!=0 is false, so condition is false. (same with 7).

Upvotes: 0

Related Questions