Generate Unique Numbers in Array

前端 未结 2 1117
忘掉有多难
忘掉有多难 2021-01-26 03:56

I\'m new to javascript and trying to get comfy with Functions, For loops, and If statements. I\'m working on a simple exercise that genera

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-26 04:41

    There are many ways to solve this problem. I am contributing my way of solving it.

    function randoNumbers(min, max) {
    
      let randomNumbers = [];
    
      for (; randomNumbers.length < 5;) {
        const value = Math.floor(Math.random() * (max - min) + +min);
        if (!randomNumbers.includes(value))
          randomNumbers.push(value);
      }
      console.log(randomNumbers);
    
    }
    
    
    
    randoNumbers(1, 10);

提交回复
热议问题