Jump to content

I'm in a JS course, so this is some practice for me. Go easy!

 

I'm trying to build a program where, depending on if useRandom is true or false, it adds two numbers together. Either random numbers, or the specific number I chose (99).

My main issue is I'm trying to toggle the useRandom variable from true to false and back again but I can't figure out what to put in that toggleRandom() function.

 

Help!

 

(ignore the comments btw)

 

var useRandom = true 

function toggleRandom() {
    return useRandom = !true
}

function specNumber() {        // function gives me a number of my choosing
    return 99 
}

function getRandom() {         // function gives me a random number
    return (Math.random() + 1)
}

function addition(a, b) {      // function adds two numbers
    console.log(a + b)
}

if (useRandom) {
    getNumber = getRandom()
} else {
    getNumber = specNumber()
}

addition(getNumber, getNumber)

toggleRandom();

addition(getNumber,getNumber)

 

Link to comment
https://linustechtips.com/topic/1508771-help-with-boolean-values-and-variables-js/
Share on other sites

Link to post
Share on other sites

10 minutes ago, Neftex said:
function toggleRandom() {
    useRandom = !useRandom
}

youre not using the return value of the function anyway, right? so the function could just assign the negated value to the variable?

This is the way

5950X/4090FE primary rig  |  1920X/1070Ti Unraid for dockers  |  200TB TrueNAS w/ 1:1 backup

Link to post
Share on other sites

10 minutes ago, Neftex said:
function toggleRandom() {
    useRandom = !useRandom
}

youre not using the return value of the function anyway, right? so the function could just assign the negated value to the variable?

OK, I made that change, but the output in the console is still the random numbers adding, even after toggling

Link to post
Share on other sites

19 minutes ago, Neftex said:

the reason why its not changing is because this part is only called once, not whenever you do the addition

if (useRandom) {
    getNumber = getRandom()
} else {
    getNumber = specNumber()
}

 

Does that mean I need to include the If statement in the addition function?

Link to post
Share on other sites

theres many ways to fix it for example

you could just copy paste it right under the toggle function call

or you could do it as part of the toggle function

or you could do it as part of the addition function (this would require more changes, because youre sending parameters to that function "before" that if statement runs)

Link to post
Share on other sites

18 hours ago, Neftex said:
function toggleRandom() {
    useRandom = !useRandom
}

youre not using the return value of the function anyway, right? so the function could just assign the negated value to the variable?

Long live languages with no syntax safeties and call it "beginner" language

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

×