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.
while (rand !== prevRand) {should bewhile (rand == prevRand) {. The way you wrote it forcesrandto be the same asprevRandwhen the loop finishes.