iT邦幫忙

0

js 取亂數及檢查陣列內相減的值

各位大神好,

目前再做一個練習, 實現取 8 個小於 31 的亂數, 並將亂數由小到大排列並後由第一個減第二個, 以此類推, 而相減結果不得<= -6, 如果<= -6 就執行 location.reload()刷新頁面.

預期結果如下:

[4, 5, 6, 11, 16, 20, 22, 22], [-1, -1, -5, -5, -4, -2, 0, NaN]

而如果想取多組情況頁面會刷新很久, 觀感不太好.
想請教, 怎麼樣精進可以不用location.reload() 就達到預期的效果呢?

附上程式碼:

function getRandom(x) {
  return Math.floor(Math.random() * x + 1);
};

function getNum() {
  let ansewer = [];
  let check = [];
  let n = 0;

  for (i = 0; i <= 7; i++) {
    let n = getRandom(31);
    if (ansewer.indexOf(n) > 0) {
      i--;
      continue;
    } else {
      ansewer.push(n);
    };
  };

  ansewer.sort((a, b) => {
    return a - b;
  });

  //檢查是否組數相減有小於等於 -6 的
  for (let i = 0; i < ansewer.length; i++) {
    check.push(ansewer[i] - ansewer[i + 1])
  }
  
  if (check.find((el) => el <= -6)) {
    location.reload()
  }

  console.log(ansewer, check);
  return ansewer;
};

getNum()

附上網址: https://jsfiddle.net/cooper081/26xrcyve/10/

Mao iT邦新手 1 級 ‧ 2021-11-21 14:59:16 檢舉
你檢查重複號碼的地方要寫 ansewer.indexOf(n) >= 0 或 ansewer.indexOf(n) > -1,不然有機會號碼重複
Cooper iT邦新手 5 級 ‧ 2021-11-21 16:06:17 檢舉
謝謝指正!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

3
fillano
iT邦超人 1 級 ‧ 2021-11-22 09:23:24
最佳解答

https://jsfiddle.net/fillano/7rbmnxe6/16/

友善介面版...感覺可以用generator做,所以用無限迴圈配合yield寫一個。

看更多先前的回應...收起先前的回應...

費大漏掉處理數字重覆的部份了...
/images/emoticon/emoticon58.gif

fillano iT邦超人 1 級 ‧ 2021-11-22 10:58:05 檢舉

我用splice取陣列元素,取出就同時刪掉,所以應該不會重複。你是指這裡?

https://ithelp.ithome.com.tw/upload/images/20211122/20001787P1dGWTWsI3.png
是我老花眼
把 check 當成 answer 了
/images/emoticon/emoticon25.gif

fillano iT邦超人 1 級 ‧ 2021-11-22 17:26:03 檢舉

/images/emoticon/emoticon37.gif

Cooper iT邦新手 5 級 ‧ 2021-11-23 20:25:34 檢舉

WOW..小弟腦補了好久..結果可以這麼簡潔明瞭 謝謝大神的思路!!

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-11-21 17:21:16

參考看看合不合用

        function getRandom(x) {
          return Math.floor(Math.random() * x + 1);
        };


        function getNum() {
          let ansewer = [];
          let check = [];
					trytimes = 0;

					while (trytimes < 20) {
          let n = 0;
				        	
          for (i = 0; i <= 7; i++) {
            let n = getRandom(31);
            if (ansewer.indexOf(n) >= 0) {
              i--;
              continue;
            } else {
              ansewer.push(n);
            };
          };

          ansewer.sort((a, b) => {
            return a - b;
          });

					//檢查是否組數相減有小於等於 -6 的
          for (let i = 0; i < ansewer.length; i++) {
            check.push(ansewer[i] - ansewer[i + 1])
          }

          if (check.find((el) => el <= -6)) {
            /* location.reload() */
            console.log('try ',++trytimes);            
            ansewer.length = 0;
            check.length = 0;
          } else {
          	trytimes = 9999;
          }
					}
          console.log(ansewer, check);
          return ansewer;
        };

        getNum()
Cooper iT邦新手 5 級 ‧ 2021-11-23 20:27:31 檢舉

謝謝指導, 這個方式試著處理後 發現比較適合也解決了我另一個小問題

我要發表回答

立即登入回答