Home > Back-end >  Javascript sort array using day of month
Javascript sort array using day of month

Time:04-10

I want to change the order of a string based on what day of the month it is.

For example, say the word is "PIZZAS", on the 1st of May it will be "ZZSIPA", and on the 2nd of May it will be "APIZZS" etc...

The code I run works for Math.random(), however when I assign it the value of the day of the month, it does not change.

    const dayOfMonth = new Date().getDate();

function shuffle(s) {
    
  var arr = s.split(''); 

for (let i = arr.length -1; i > 0; i--) {
  let j = Math.floor(dayOfMonth * i)
  let k = arr[i]
  arr[i] = arr[j]
  arr[j] = k
}
    
  s = arr.join(''); 
  return s; // Return shuffled string
    
}
var s = shuffle("PIZZAS");

CodePudding user response:

Math.random() returns a float between 0 and 1, new Date().getDate() returns an integer between 1 and 31, so if you want to get the same behavior you should divide the day of month by 31.

But note that this pseudo-random number generator is not very good and you'd probably get less predictable results with something from question about seeding JS random number generator.

CodePudding user response:

By using 'Math.sin(dayOfMonth ) * 10000', it's solved the issue.

var dayOfMonth = new Date().getDate();

function random() {
    var x = Math.sin(dayOfMonth  ) * 10000;
    return x - Math.floor(x);
}

function shuffle(s) {
    
  var arr = s.split(''); 

for (let i = arr.length -1; i > 0; i--) {
  let j = Math.floor(random() * i)
  let k = arr[i]
  arr[i] = arr[j]
  arr[j] = k
}
    
  s = arr.join(''); 
  return s; // Return shuffled string
    
}
var s = shuffle("PIZZAS");

CodePudding user response:

Your shuffle is failed because you are swapping undefined.

I have debugged your code, the implementation of shuffle(s) function remains unchanged.

And the shuffle_new(s) will fix the problem of swapping undefined by using modulo operator: let j = Math.floor(dayOfMonth * i) % arr.length, so that arr[j] never returns undefined.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shuffle Demo</title>
</head>

<body>
    <script>
        const writeln = (text = '') => document.writeln(text   '<br/>');
    </script>
    <script>
        const dayOfMonth = new Date().getDate();

        function shuffle(s) {
            var arr = s.split('');
            for (let i = arr.length - 1; i >= 0; i--) {
                writeln();
                let j = Math.floor(dayOfMonth * i)
                writeln('j:'   j);
                let k = arr[i]
                writeln('k:'   k);
                arr[i] = arr[j]
                writeln('arr[j]:'   arr[j]);
                arr[j] = k
                writeln('arr:'   arr);
            }

            s = arr.join('');
            return s; // Return shuffled string

        }

        function shuffle_new(s) {
            var arr = s.split('');
            for (let i = arr.length - 1; i >= 0; i--) {
                writeln();
                let j = Math.floor(dayOfMonth * i) % arr.length
                writeln('j:'   j);
                let k = arr[i]
                writeln('k:'   k);
                arr[i] = arr[j]
                writeln('arr[j]:'   arr[j]);
                arr[j] = k
                writeln('arr:'   arr);
            }

            s = arr.join('');
            return s; // Return shuffled string
        }

        writeln('dayOfMonth:'   dayOfMonth);
        writeln('process: shuffle("PIZZAS")');
        var s = shuffle("PIZZAS");
        writeln('output:'   s);

        writeln();
        writeln();
        writeln();
        writeln();
        writeln('process: shuffle_new("PIZZAS")');
        var s = shuffle_new("PIZZAS");
        writeln('output:'   s);
    </script>
</body>

</html>

  • Related