Jump to content
function generateDivision() {
            qnum++;
            console.log("Question #" + qnum);
            var d1 = Math.floor(Math.random() * 100);
            var d2 = Math.floor(Math.random() * 100);
            result = prompt(d1 + " / " + d2 + " = " + "?" );
            if(d1 / d2 == Math.floor(result)){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }
            
        }  

Hey guys i made this and I was wondering how to change it to work for my assignment the instructions are to "When handling the division arithmetic operation, ensure that the second argument isn’t a 0 and that the resulting answer is a whole number (hint: you may find a while loop and the modulus operator “%” useful here)." how would i go about doing that? Thanks for any help!

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/
Share on other sites

Link to post
Share on other sites

13 minutes ago, Cookiecrumbles222 said:

function generateDivision() {
            qnum++;
            console.log("Question #" + qnum);
            var d1 = Math.floor(Math.random() * 100);
            var d2 = Math.floor(Math.random() * 100);
            result = prompt(d1 + " / " + d2 + " = " + "?" );
            if(d1 / d2 == Math.floor(result)){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }
            
        }  

Hey guys i made this and I was wondering how to change it to work for my assignment the instructions are to "When handling the division arithmetic operation, ensure that the second argument isn’t a 0 and that the resulting answer is a whole number (hint: you may find a while loop and the modulus operator “%” useful here)." how would i go about doing that? Thanks for any help!

function generateDivision() {
            qnum++;
            console.log("Question #" + qnum);
            var d1 = Math.floor(Math.random() * 100);
            var d2 = Math.floor(Math.random() * 100);
            
            while (d2 > d1){
            var d1 = Math.floor(Math.random() * 100);
            }
            
            while (d2 == 0){
            var d2 = Math.floor(Math.random() * 100);        
            }
            
            result = prompt(d1 + " / " + d2 + " = " + "?" );
            
            if(d1 / d2 == Math.floor(result)){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }
            
        }  

ive narrowed it down to this, but how do i make the two numbers divide to = something whole?

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11894643
Share on other sites

Link to post
Share on other sites

Follow the hint and find out what the modulus operator does and then consider that in relation to the instructions.

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11894648
Share on other sites

Link to post
Share on other sites

6 minutes ago, keskparane said:

Follow the hint and find out what the modulus operator does and then consider that in relation to the instructions.

function generateDivision() {
            qnum++;
            console.log("Question #" + qnum);
            var d1 = Math.floor(Math.random() * 100);
            var d2 = Math.floor(Math.random() * 100);
            
            while (d2 > d1){
            d1 = Math.floor(Math.random() * 100);
            }
            
            while (d2 == 0){
            d2 = Math.floor(Math.random() * 100);        
            }
            
            while(d1 % d2 != 0){
            d1 = Math.floor(Math.random() * 100);
            d2 = Math.floor(Math.random() * 100);    
            }
            
            result = prompt(d1 + " / " + d2 + " = " + "?" );
            
            if(d1 / d2 == Math.floor(result)){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }
            
        } 

i understand what it does i just don't know how to include it without being terribly messy like this ^^

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11894657
Share on other sites

Link to post
Share on other sites

27 minutes ago, Cookiecrumbles222 said:

function generateDivision() {
            qnum++;
            console.log("Question #" + qnum);
            var d1 = Math.floor(Math.random() * 100);
            var d2 = Math.floor(Math.random() * 100);
            result = prompt(d1 + " / " + d2 + " = " + "?" );
            if(d1 / d2 == Math.floor(result)){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }
            
        }  

Hey guys i made this and I was wondering how to change it to work for my assignment the instructions are to "When handling the division arithmetic operation, ensure that the second argument isn’t a 0 and that the resulting answer is a whole number (hint: you may find a while loop and the modulus operator “%” useful here)." how would i go about doing that? Thanks for any help!

 

result is not a number. you cant compare d1 / d2 to it.

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11894662
Share on other sites

Link to post
Share on other sites

take x % y = z

modulo operator returns the remainder when x is divided by y

so if x = 5, y = 2, z will be 1 since 5 / 2 = 2 remainder 1.

 

Work from that info.

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11894665
Share on other sites

Link to post
Share on other sites

1 hour ago, Cookiecrumbles222 said:

i understand what it does i just don't know how to include it without being terribly messy like this ^^

so break it down. first you

2 hours ago, Cookiecrumbles222 said:

... ensure that the second argument isn’t a 0....

then if it isn't you make sure that

2 hours ago, Cookiecrumbles222 said:

... the resulting answer is a whole number....

so you would need to compare the result of the division with something

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11894908
Share on other sites

Link to post
Share on other sites

11 hours ago, Cookiecrumbles222 said:

function generateDivision() {
            qnum++;
            console.log("Question #" + qnum);
            var d1 = Math.floor(Math.random() * 100);
            var d2 = Math.floor(Math.random() * 100);
            
            while (d2 > d1){
            d1 = Math.floor(Math.random() * 100);
            }
            
            while (d2 == 0){
            d2 = Math.floor(Math.random() * 100);        
            }
            
            while(d1 % d2 != 0){
            d1 = Math.floor(Math.random() * 100);
            d2 = Math.floor(Math.random() * 100);    
            }
            
            result = prompt(d1 + " / " + d2 + " = " + "?" );
            
            if(d1 / d2 == Math.floor(result)){
            console.log("Correct!");
            }
            else{
            console.log("Incorrect!");
            }
            
        } 

i understand what it does i just don't know how to include it without being terribly messy like this ^^

I see a problem here: If it fails the whole number check and you're generating new random numbers, you're not making sure it passes the checks you did earlier.

 

I'll let you stew on how to resolve this but this what I suggest: Make it impossible for the divisor to be 0 or greater than the dividend (d1) in the first place.

 

If you want a cheat:

Spoiler

var d1 = Math.floor(Math.random() * 100);
var d2 = Math.floor(Math.random() * d1) + 1; 
/* Because Math.random produces a number between 0 inclusive and 1 exclusive, 
   Math.random () * d1 will never be d1, so adding 1 at the end makes it so 
   you cannot have either 0 or d1 + 1*/

while ( d1 % d2 !== 0 ) {
  d1 = Math.floor(Math.random() * 100);
  d2 = Math.floor(Math.random() * d1) + 1;
}

 

 

Link to comment
https://linustechtips.com/topic/987238-division-in-javascript/#findComment-11895965
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×