0

I have 2 functions on a single .js file.

function notUsed(id) {
        //default to false because if true then id not being used and good for new user
        var notInUse = false;

        console.log(notInUse);
        return !notInUse;   
    }

function generateID() {

        //number of zeros represents the number of digits in id code
        const SIZEOFID = 10000000;
        const ID_DIGITS = 7;
        //letter to start id for non los rios people
        const STRTOFID = "C";
        //variable to hold finished id code & variable to hold 7 digit of id code
        var id, idNum;

        //loop to make sure id contains 7 digits and 1 letter and not used already
        do {
            idNum = Math.round(Math.random() * SIZEOFID);
            idNum.toString();
            id = (STRTOFID + idNum);
        }while(id.length != (ID_DIGITS+1) && notUsed(id));
        console.log(id);
    }

When I call generateID() from my web page, the ID gets logged but false does not get logged(Obviously notUsed function is incomplete). However, if I call each function separately from my web page, both the ID and false get logged. How can I fix or work around this issue? Any comments help.

5
  • it works for me Commented Aug 17, 2017 at 14:52
  • @Dij the ID getting logged is just a random ID with a single character and 7 digits Commented Aug 17, 2017 at 14:52
  • @MarcoSalerno when calling generateID() from inside a script tag on a web page? Commented Aug 17, 2017 at 14:53
  • 1
    Apparantly notUsed(id) is not executed. id.length != (ID_DIGITS+1) returns false so second operand is not evaluated Commented Aug 17, 2017 at 14:53
  • I tried it inside stack overflow's snippets Commented Aug 17, 2017 at 15:06

2 Answers 2

2

The logical and is short-circuiting because the first comparison is false. The second never gets evaluated, which is why it's not logging. It's not being called.

Sign up to request clarification or add additional context in comments.

6 Comments

How can I fix that if it is possible?
@NickPavini You probably want to use || instead of &&
Well if the purpose is to verify that the ID as generated is not used by another user, then just call notUsed only. Your code as written will guarantee the length of idNum to be 8 characters, I don't think you need that check.
@JohanKarlsson but I need both circumstances to be true.
@NickPavini !(A && B) == !A || !B
|
1

It`s happened because first condition in while id.length != (ID_DIGITS+1) return false, if first condition return false next conditions will not be called

Example:

function imreturnTrue() {
  console.log('imreturnTrue');
  return true
};

function impreturnFalse() {
   console.log('impreturnFalse');
   return false
};

function imreturnTrue1() {
    console.log('imreturnTrue1');
    return true
};
let example = imreturnTrue() && impreturnFalse() && imreturnTrue1();
// imreturnTrue impreturnFalse

let example1 = imreturnTrue() && imreturnTrue1() && impreturnFalse() ; 
// imreturnTrue imreturnTrue1 impreturnFalse

let example2 = impreturnFalse() && imreturnTrue() && imreturnTrue1() ; 
// impreturnFalse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.