help with myfun()

linkzerone

Well-Known Member
OP
Newcomer
Joined
Oct 24, 2016
Messages
47
Trophies
0
Age
26
XP
97
Country
Canada
Im making a fun randomizer for a little bit of practice im, stuck on the if statment were if there is an double randomize that index. i got it working for the first and last index by comparing the middle index however I now want to compare the two ends to each. after that I have to see if the random number is the same in every index I plan on using the dom later on but for now im console.loging to see the results in the deloper tools thanks for your help.

var abcs = ["a", "b", "c"];

/*takes the length of an array and gives each index
a random whole number and stores the random numbers
in an empty array*/
function shuffle(array, randomIndex){

/*the randomIndex values are stored in this empty
array*/
var arrayIndex = [];

//makes a random whole number between 0 and 9
function randomIndex(){
return Math.floor(Math.random() * 10);
}

/*loops through an array and stores its value in an
empty array*/
for(var i = 0; i < array.length; i++){
arrayIndex.push(randomIndex(array));
//checks to see if there are doubles
if(arrayIndex === arrayIndex[i - 1]){
console.log("double");
}
}
console.log(arrayIndex);
}
 

Futurdreamz

Well-Known Member
Member
Joined
Jun 15, 2014
Messages
2,276
Trophies
1
Age
32
XP
2,128
Country
Canada
Something like this? I haven't coded in years though...

Code:
var j;
for(var i = 0; i < array.length; i++){
for(j = i + 1; j <= array.length; j++){
//checks to see if there are doubles
if(arrayIndex[i] === arrayIndex[j]){
console.log("double");
}
}
}
 

linkzerone

Well-Known Member
OP
Newcomer
Joined
Oct 24, 2016
Messages
47
Trophies
0
Age
26
XP
97
Country
Canada
Something like this? I haven't coded in years though...

Code:
var j;
for(var i = 0; i < array.length; i++){
for(j = i + 1; j <= array.length; j++){
//checks to see if there are doubles
if(arrayIndex[i] === arrayIndex[j]){
console.log("double");
}
}
}
thanks for the help however it didnt work
 

Essometer

Needs data
Member
Joined
Oct 22, 2010
Messages
732
Trophies
1
Age
33
Location
Bielefeld
Website
none.de
XP
3,590
Country
Germany
Just for clarification, you want to fill an array with random numbers, but in that array none of these numbers should be duplicate?

Also, "that didn't work" isn't very helpful when troubleshooting. Please state at least what didn't work.
 
Last edited by Essometer,

linkzerone

Well-Known Member
OP
Newcomer
Joined
Oct 24, 2016
Messages
47
Trophies
0
Age
26
XP
97
Country
Canada
Just for clarification, you want to fill an array with random numbers, but in that array none of these numbers should be duplicate?

Also, "that didn't work" isn't very helpful when troubleshooting. Please state at least what didn't work.
yes the empty array needs to be random in every index with no repeats and it loops through the randomIndex array but when I cosole.log("double") it says double with every index instead of console.long every duplicate
 

Essometer

Needs data
Member
Joined
Oct 22, 2010
Messages
732
Trophies
1
Age
33
Location
Bielefeld
Website
none.de
XP
3,590
Country
Germany
Code:
arrayToRandomize = ["a", "b", "c"];

function randomIndex(){
return Math.floor(Math.random() * 10);
}


function randomize(array){

    var arrayIndex = [];

    for (index = 0; index < array.length; index++){

        arrayIndex.push(randomIndex());
        var toCheck = arrayIndex[index];

        for (innerIndex = 0; innerIndex < arrayIndex.length-1; innerIndex++){
            if (toCheck === arrayIndex[innerIndex]){
                console.log('double');
            }
        }
       
    }
    console.log(arrayIndex);
} 

randomize(arrayToRandomize);

If I understand you correctly, this should do the trick.
 

linkzerone

Well-Known Member
OP
Newcomer
Joined
Oct 24, 2016
Messages
47
Trophies
0
Age
26
XP
97
Country
Canada
Code:
arrayToRandomize = ["a", "b", "c"];

function randomIndex(){
return Math.floor(Math.random() * 10);
}


function randomize(array){

    var arrayIndex = [];

    for (index = 0; index < array.length; index++){

        arrayIndex.push(randomIndex());
        var toCheck = arrayIndex[index];

        for (innerIndex = 0; innerIndex < arrayIndex.length-1; innerIndex++){
            if (toCheck === arrayIndex[innerIndex]){
                console.log('double');
            }
        }
      
    }
    console.log(arrayIndex);
}

randomize(arrayToRandomize);
yes thats it now i just need to figure out how to pick out the index and randomize that index agin
If I understand you correctly, this should do the trick.
 

linkzerone

Well-Known Member
OP
Newcomer
Joined
Oct 24, 2016
Messages
47
Trophies
0
Age
26
XP
97
Country
Canada
new problem sometimes doubles slip past the logic and wont reshuffle the index in the if statment

//makes a random whole number between 0 and 9
arrayToRandomize = ["a", "b", "c"];

function randomIndex(){
return Math.floor(Math.random() * 10);
}

function randomize(array){

var arrayIndex = [];

for (index = 0; index < array.length; index++){

arrayIndex.push(randomIndex());
var toCheck = arrayIndex[index];

for (innerIndex = 0; innerIndex < arrayIndex.length-1; innerIndex++){
if (toCheck === arrayIndex[innerIndex]){
arrayIndex.pop(toCheck);
console.log(toCheck + " removed");
arrayIndex.push(randomIndex());
console.log(toCheck + " add");
}
}

}
console.log(arrayIndex);
}

randomize(arrayToRandomize);

--------------------- MERGED ---------------------------

new problem sometimes doubles slip past the logic and wont reshuffle the index in the if statment

//makes a random whole number between 0 and 9
arrayToRandomize = ["a", "b", "c"];

function randomIndex(){
return Math.floor(Math.random() * 10);
}

function randomize(array){

var arrayIndex = [];

for (index = 0; index < array.length; index++){

arrayIndex.push(randomIndex());
var toCheck = arrayIndex[index];

for (innerIndex = 0; innerIndex < arrayIndex.length-1; innerIndex++){
if (toCheck === arrayIndex[innerIndex]){
arrayIndex.pop(toCheck);
console.log(toCheck + " removed");
arrayIndex.push(randomIndex());
console.log(toCheck + " add");
}
}

}
console.log(arrayIndex);
}

randomize(arrayToRandomize);
looks like only the first and last array its really close
 

linkzerone

Well-Known Member
OP
Newcomer
Joined
Oct 24, 2016
Messages
47
Trophies
0
Age
26
XP
97
Country
Canada
new problem sometimes doubles slip past the logic and wont reshuffle the index in the if statment

//makes a random whole number between 0 and 9
arrayToRandomize = ["a", "b", "c"];

function randomIndex(){
return Math.floor(Math.random() * 10);
}

function randomize(array){

var arrayIndex = [];

for (index = 0; index < array.length; index++){

arrayIndex.push(randomIndex());
var toCheck = arrayIndex[index];

for (innerIndex = 0; innerIndex < arrayIndex.length-1; innerIndex++){
if (toCheck === arrayIndex[innerIndex]){
arrayIndex.pop(toCheck);
console.log(toCheck + " removed");
arrayIndex.push(randomIndex());
console.log(toCheck + " add");
}
}

}
console.log(arrayIndex);
}

randomize(arrayToRandomize);

--------------------- MERGED ---------------------------


looks like only the first and last array its really close
I notcied 3 results that brake it 1: the beginning of the array
2: the end of the array
3:both the beginning and the end of the array
4:the bigger arrays tend to manage to suffle doubles in
 

Essometer

Needs data
Member
Joined
Oct 22, 2010
Messages
732
Trophies
1
Age
33
Location
Bielefeld
Website
none.de
XP
3,590
Country
Germany
Array.pop doesn't take an argument, it just removes the last index.

If you find an duplicated value, you need to pop it, generate another random value, push it into the array
and then check for duplicates. That means the inner for loop need to run again.
In the current state for for loop only pops one duplicate, but if the second push is a duplicate as well, the loop is already finished.

Also, if you post code, please use [ CODE][/CODE] (without the first space), so the code become much more readable.
 
Last edited by Essometer,

Essometer

Needs data
Member
Joined
Oct 22, 2010
Messages
732
Trophies
1
Age
33
Location
Bielefeld
Website
none.de
XP
3,590
Country
Germany
Code:
arrayToRandomize = ["a", "b", "c", "d", "e", "f", "g", "h", "j"];

function randomIndex(){
return Math.floor(Math.random() * 10);
}


function randomize(array){

    var arrayIndex = [];

    for (index = 0; index < array.length; index++){

        arrayIndex.push(randomIndex());
        var toCheck = arrayIndex[index];
        var duplicates = true;
        while(duplicates){
            duplicates = false
            for (innerIndex = 0; innerIndex < arrayIndex.length-1; innerIndex++){
                if (toCheck === arrayIndex[innerIndex]){
                    duplicates = true;
                    arrayIndex.pop();
                    arrayIndex.push(randomIndex());
                    var toCheck = arrayIndex[index];
                }
            }

        }
    
    
    }
    console.log(arrayIndex);
}

randomize(arrayToRandomize);

Ok, got bored and and just threw it together. This is in no way optimized but gets the job done.

EDIT: Just realized that the .indexof function exsits...
 
Last edited by Essometer,

Futurdreamz

Well-Known Member
Member
Joined
Jun 15, 2014
Messages
2,276
Trophies
1
Age
32
XP
2,128
Country
Canada
thanks for the help however it didnt work
I wouldn't be surprised. Like I said it's been years and I didn't know what some of the code meant. However the double for loop DOES check every array entry, no matter if there's 3 or 30. It was only intended as an example, to you to try to work into your code.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: So negative