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...

3
  • 1
    AFAIK all the variables you're using are global. Commented Mar 15, 2020 at 16:55
  • @evolutionxbox the only variable that isn't global is arr, since it's a parameter. Everything else is global. Commented Mar 15, 2020 at 17:02
  • @VLAZ you're right! That's a parameter. (I didn't spot that) Commented Mar 15, 2020 at 17:06

2 Answers 2

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"]

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

1 Comment

@sMohammad14 But the code that you are showing to use does not either use let, const or var.
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);

3 Comments

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
functions don't have to have return statements. You can add one if you want, but you don't really need it here.
please introduce to me a reference (with easy English) about type of calls in js @DCR

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.