How to pick a random function from a list of function

Currently, I’m trying to take a random function from a list of function so I can call on a random card when a dot have reached a certain checkpoint via arrow keys. Current I have failed, having visited many sources and following peoples answers I have messed up most of my code. It probably has a ton of errors. Anyway, here are the main things:

HTML

<div class="cord1"></div>
<div class="cord2"></div>

<div class="man"></div>

<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>
<div class="block 1"></div>

JS

func1() {
   console.log("Func1 works")
}

func2() {
   console.log("Func2 works")
}


func3() {
   console.log("Func3 works")
}


func4() {
   console.log("Func4 works")
}

window.addEventListener("keyup", (e) => {
   switch (e.key){
      case "ArrowLeft":

      var array = [
         func1
         func2
         func3
         func4
      ]

     function solRandTest() {
        var i = array.length;
        var rand = sol[Math.floor(Math.random() * i)];
        return rand;
      }

      case "ArrowRight":
      var array = [
         func1
         func2
         func3
         func4
      ]

     function solRandTest() {
        var i = array.length;
        var rand = sol[Math.floor(Math.random() * i)];
        return rand;
      }


      case "ArrowUp":

      var array = [
         func1
         func2
         func3
         func4
      ]

     function solRandTest() {
        var i = array.length;
        var rand = sol[Math.floor(Math.random() * i)];
        return rand;
      }
      case "ArrowDown":

      var array = [
         func1
         func2
         func3
         func4
      ]

     function solRandTest() {
        var i = array.length;
        var rand = sol[Math.floor(Math.random() * i)];
        return rand;
      }
  }

}
1 Like

You don’t tell us what’s “not working”, so here’s me guessing.

Taking one instance of arrow logic:

You define the function to get a random function, but don’t invoke it. And nothing will happen with your returned function unless you invoke that.

So I’d add the following lines to the case block:

var randomFunc = solRandTest()  // get a random function
randomFunc()                    // execute the random function

Some links which might be helpful.