help with myfun()

Discussion in 'Computer Programming, Emulation, and Game Modding' started by linkzerone, Apr 7, 2017.

  1. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    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);
    }
     
  2. Futurdreamz

    Futurdreamz GBAtemp Advanced Maniac

    Member
    1,681
    942
    Jun 15, 2014
    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");
    }
    }
    }
    
     
  3. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    Canada
    thanks for the help however it didnt work
     
  4. Essometer

    Essometer GBAtemp Regular

    Member
    284
    188
    Oct 22, 2010
    Germany
    Enger
    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, Apr 8, 2017
  5. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    Canada
    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
     
  6. Essometer

    Essometer GBAtemp Regular

    Member
    284
    188
    Oct 22, 2010
    Germany
    Enger
    .
     
    Last edited by Essometer, Apr 8, 2017
  7. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    Canada
    is what i want possabile
     
  8. Essometer

    Essometer GBAtemp Regular

    Member
    284
    188
    Oct 22, 2010
    Germany
    Enger
    Yes, I just was to triggerhappy.
     
  9. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    Canada
    ok right now im trying to wrote different functions and doing exsperiments
     
  10. Essometer

    Essometer GBAtemp Regular

    Member
    284
    188
    Oct 22, 2010
    Germany
    Enger
    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.
     
  11. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    Canada
     
  12. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    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);

    — Posts automatically merged - Please don't double post! —

    looks like only the first and last array its really close
     
  13. linkzerone
    OP

    linkzerone Member

    Newcomer
    47
    0
    Oct 24, 2016
    Canada
    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
     
  14. Essometer

    Essometer GBAtemp Regular

    Member
    284
    188
    Oct 22, 2010
    Germany
    Enger
    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, Apr 8, 2017
  15. Essometer

    Essometer GBAtemp Regular

    Member
    284
    188
    Oct 22, 2010
    Germany
    Enger
    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, Apr 8, 2017
  16. Futurdreamz

    Futurdreamz GBAtemp Advanced Maniac

    Member
    1,681
    942
    Jun 15, 2014
    Canada
    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.