[英]How do I colour a number from a Javascript variable when it hits a certain number, within the Code.org HTML Web Lab?
[英]how do i stop the generator from generating numbers when it hits an exact number?
我想問一下如何讓生成器在生成一個甚至使用循環和 arrays 的數字時中斷。我嘗試使用 if (i%2) {break} 來做到這一點,但它對我不起作用。 另外能不能幫我把生成的負數都放到arrays結尾? 感謝您的幫助。 這是我的 javascript 代碼
<script>
var text = "";
let mas = [];
const y = 200;
const y1 = -20;
for (let i = 0; i != 15; i++) {
mas.push(Math.floor(Math.random() * y) + y1);
mas.sort();
}
document.getElementById("demo").innerHTML = mas;
</script>
您可以通過查看隨機值並按符號排序來破壞數組。
const mas = [], y = 200, y1 = -20; for (let i = 0; i < 15; i++) { const value = Math.floor(Math.random() * y) + y1; mas.push(value); if (value % 2 === 0) break; } mas.sort((a, b) => Math.sign(b) - Math.sign(a)); console.log(...mas);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.