0

i write this code for shuffle array:

function shuffle(arr) {
    for (i = 0; i < arr.length; i++) {
        x = Math.floor(Math.random() * arr.length);
        y = Math.floor(Math.random() * arr.length);
        if (x === y) { //for dont change arr[i] with arr[i]!!!
            continue;
        }
        temp0 = arr[x];
        arr[x] = arr[y];
        arr[y] = temp0;
    }
    return arr
}

it work correctly. but my problem is, this function not global, i explain it with a example:

    sampleArray=["a", "b", "c", "d"];

    shuffle(sampleArray); //only run function
    console.log (sampleArray); // output NOT shuffled. ==>> ["a", "b", "c", "d"]
    console.log (shuffle(sampleArray)); //output shuffled. my be ["d", "a", "c", "b"] or ...

in main code i cant declare sampleArray nested in shuffle function...

sMohammad14
  • 33
  • 1
  • 6

2 Answers2

1

let, const and var context

If you do not define your variables with let, const or var, they will be scoped as global variables.

You have in here a good tutorial about javascript variables and scoping.

But resuming:

  • If you do not put let, const or var before your variable definition will be always created as a global variable.
  • If you use var before, the variable will be created as function scoped.
  • If you use let before, the variable will be block scoped (between two {}).
  • If you use const before, the same rules of let are applied with an exception of you can't reasign a new value to the variable.

Moreover!

Non-permitive values such as arrays, in javascript, are passed to function as a reference, meaning if you change any array value inside a function, the original variable will change is value as well (For more info, check this link). This is why your sampleArray are being changed: because you change the arr variable that references the sampleArray in the shuffle function.

Example Time!

For this to work you could do a deepcopy of the arr inside the shuffle function like this:

function shuffle(arr) {
    //deep copy
    const deepCopyArray = JSON.parse(JSON.stringify(arr));
    for (i = 0; i < deepCopyArray.length; i++) {
        x = Math.floor(Math.random() * deepCopyArray.length);
        y = Math.floor(Math.random() * deepCopyArray.length);
        if (x === y) { //for dont change arr[i] with arr[i]!!!
            continue;
        }
        temp0 = deepCopyArray[x];
        deepCopyArray[x] = deepCopyArray[y];
        deepCopyArray[y] = temp0;
    }
    return deepCopyArray
}

sampleArray=["a", "b", "c", "d"];
shuffle(sampleArray); //only run function
console.log (sampleArray); // output NOT shuffled. ==>> ["a", "b", "c", "d"]
console.log (shuffle(sampleArray)); //output shuffled. my be ["d", "a", "c", "b"]
Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
0

you have a typo in your code. length NOT lengt. Also, since arrays are passed by reference you don't need a return

function shuffle(arr) {

    for (i = 0; i < arr.length; i++) {
        var  x = Math.floor(Math.random() * arr.length);
        var  y = Math.floor(Math.random() * arr.length);
        
        if (x === y) { //for dont change arr[i] with arr[i]!!!
            continue;
        }
        temp0 = arr[x];
        arr[x] = arr[y];
        arr[y] = temp0;
    }
    
    
}
sampleArray=["a", "b", "c", "d"];



shuffle(sampleArray);


console.log(sampleArray);
DCR
  • 14,737
  • 12
  • 52
  • 115
  • yes, work for me. but a question, is this standard code!? a function without return!?. i ask about it because when console.log (shuffle(sampleArray) in console show undefined – sMohammad14 Mar 15 '20 at 17:50
  • 1
    functions don't have to have return statements. You can add one if you want, but you don't really need it here. – DCR Mar 15 '20 at 17:54
  • please introduce to me a reference (with easy English) about type of calls in js @DCR – sMohammad14 Mar 16 '20 at 00:06