1

I am trying to take random username and password from an array, and checking whether current username password is same as previous username password. If it is same, i want to take different user id, pass from the array. I wrote below code, but I it also picks up same username passwords.

let usernames = ["uname1", "uname2"],
        passwords = ["pass1", "pass1"];

    let rand, prevRand;
    prevRand = _.random(0, usernames.length - 1);
    rand = _.random(0, usernames.length - 1);

    console.log("rand " + rand)
    console.log("prevrand " + prevRand)


    if (rand !== prevRand) {
        console.log("uname!==: " + usernames[rand])
        console.log("pass!==: " + passwords[rand])
        prevRand = rand;
    } else {
        while (rand !== prevRand) {
            rand = _.random(0, usernames.length - 1);
            console.log("newrand " + rand)

        }
        prevRand = rand;
        console.log("unamenew " + usernames[rand])
        console.log("passnew " + passwords[rand])

    }

Please correct my logic here..Thanks.

2
  • Please add the lodash or underscorejs tag to indicate which library you're using. Commented Feb 4, 2018 at 6:48
  • while (rand !== prevRand) { should be while (rand == prevRand) {. The way you wrote it forces rand to be the same as prevRand when the loop finishes. Commented Feb 4, 2018 at 6:49

2 Answers 2

1

Your while (rand !== prevRand) loop is never executed because it is inside the else statement wich means that rand == prevRand. Try changing the while loop to do..while.

Check the example (I've replaced _.random with Math.random to run without external libraries)

let usernames = ["uname1", "uname2", "uname3", "uname4"],
    passwords = ["passw1", "passw2", "passw3", "passw4"];

let rand, prevRand;
prevRand = Math.floor(Math.random() * (usernames.length));
rand = Math.floor(Math.random() * (usernames.length));

console.log("rand " + rand)
console.log("prevrand " + prevRand)


if (rand !== prevRand) {
    console.log("uname " + usernames[rand])
    console.log("pass " + passwords[rand])
    prevRand = rand;
} else {
    do {
        rand = Math.floor(Math.random() * (usernames.length));
        console.log("newrand " + rand)
    } while (rand == prevRand);
    prevRand = rand;
    console.log("unamenew " + usernames[rand])
    console.log("passnew " + passwords[rand])
}

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

2 Comments

So after making the changes you said, after the if/else loop, if I use the value of rand to get the username in another variable it will be correct?
@AforApple After the loop rand will hold other value than prevRand, so usernames[rand] will differ from usernames[prevRand]. As well as passwords
0

rand === prevRand is true in the else case, so it will never enter the while cycle.

The cycle, instead, must loop while rand is equal to prevRand

  while (rand === prevRand) {
        rand = _.random(0, usernames.length - 1);
        console.log("newrand " + rand)
 }

Each time they are equal a new value to rand should be given, so they can be different.

1 Comment

So after making the changes you said, after the if/else loop, if I use the value of rand to get the username in another variable it will be correct?

Your Answer

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