如何在JavaScript中生成并输出列表中的最小随机数?

huangapple go评论53阅读模式
英文:

How to generate and output the smallest random number from a list in JavaScript?

问题

///before

let a = Math.floor(Math.random() * 100+1)
let b = Math.floor(Math.random() * 100+1)
let c = Math.floor(Math.random() * 100+1)
const abc = [a,b,c]
result = Math.min(...abc);
console.log(result)// works!

///after

function test() {
  let a = Math.floor(Math.random() * 100+1)
  let b = Math.floor(Math.random() * 100+1)
  let c = Math.floor(Math.random() * 100+1)
  let abc = [];
if((a==!b)&& (b==!c)){
   abc.push(a);
   abc.push(b);
   abc.push(c);
   const num = Math.min(...abc);
   console.log(num);
   
  } else{  
  a = Math.floor(Math.random() * 100+1)
  b = Math.floor(Math.random() * 100+1)
  c = Math.floor(Math.random() * 100+1)
      }
}
 //return Math.floor(Math.random() * (max - min + 1)) + min; //최댓값도 포함, 최솟값도 포함

test();

嘿,朋友们!我尝试创建一个函数,它从1到100中选择三个随机数并输出最小的数。你能帮我识别一下我的代码中是否有任何问题吗?我编写了这段代码来选择不重复的数字,但它不起作用 如何在JavaScript中生成并输出列表中的最小随机数? 帮帮我。

英文:

///before

let a = Math.floor(Math.random() * 100+1)
let b = Math.floor(Math.random() * 100+1)
let c = Math.floor(Math.random() * 100+1)
const abc = [a,b,c]
result = Math.min(...abc);
console.log(result)// works!

///after

function test() {
  let a = Math.floor(Math.random() * 100+1)
  let b = Math.floor(Math.random() * 100+1)
  let c = Math.floor(Math.random() * 100+1)
  let abc = [];
if((a==!b)&&(b==!c)){
   abc.push(a);
   abc.push(b);
   abc.push(c);
   const num = Math.min(...abc);
   console.log(num);
   
  } else{  
  a = Math.floor(Math.random() * 100+1)
  b = Math.floor(Math.random() * 100+1)
  c = Math.floor(Math.random() * 100+1)
      }
}
 //return Math.floor(Math.random() * (max - min + 1)) + min; //최댓값도 포함, 최솟값도 포함

test();

Hey, friends! I've tried to create a function that selects three random numbers from 1 to 100 and outputs the smallest number. Could you please help me identify any issues in my code?
I wrote this code to select non-repeating numbers, but it's not working
如何在JavaScript中生成并输出列表中的最小随机数? help me

答案1

得分: 2

你可以使用集合和数组,但其中的元素必须是唯一的。

var abc = new Set([]);

while(abc.size != 3){
   abc.add(Math.floor(Math.random() * 101));
}

console.log(Math.min(...Array.from(abc.values())));
英文:

You can use sets,arrays but with uniqe things
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var abc = new Set([]);

while(abc.size != 3){
   abc.add(Math.floor(Math.random() * 101));
}

console.log(Math.min(...Array.from(abc.values())));

<!-- end snippet -->

答案2

得分: 0

你可以使用一个 while 循环。

function test() {
  let abc = [];
  while (abc.length < 3) {
    let n = Math.floor(Math.random() * 100 + 1);
    if (!abc.includes(n)) abc.push(n);
  }
  return Math.min(...abc);
}
英文:

You can use a while loop

function test() {
  let abc = [];
  while (abc.length &lt; 3) {
    let n = Math.floor(Math.random() * 100 + 1);
    if (!abc.includes(n)) abc.push(n);
  }
  return Math.min(...abc);
}

答案3

得分: 0

以下是翻译好的代码部分:

function selectSmallestNumber() {
  var numbers = [];
  
  // 生成三个随机数并添加到列表中
  for (var i = 0; i < 3; i++) {
    var randomNumber = Math.floor(Math.random() * 100) + 1; // 1到100之间的随机数
    numbers.push(randomNumber);
  }
  
  // 在列表中找到最小的数
  var smallestNumber = Math.min(...numbers);
  
  return smallestNumber;
}

var result = selectSmallestNumber();
console.log(result);

此代码中,selectSmallestNumber 函数初始化一个空数组 numbers。然后,它使用 Math.random()Math.floor() 函数生成三个介于1和100之间的随机数,并使用 push 方法将它们添加到 numbers 数组中。

为了找到数组中最小的数,使用了 Math.min() 函数以及展开运算符 ... 将数组中的数作为单独的参数传递给函数。

最后,从函数中返回最小的数,并使用 console.log() 打印到控制台上。

当你运行这段代码时,它将选择三个随机数并输出最小的一个。

英文:
function selectSmallestNumber() {
  var numbers = [];
  
  // Generate three random numbers and add them to the list
  for (var i = 0; i &lt; 3; i++) {
    var randomNumber = Math.floor(Math.random() * 100) + 1; // Random number between 1 and 100
    numbers.push(randomNumber);
  }
  
  // Find the smallest number in the list
  var smallestNumber = Math.min(...numbers);
  
  return smallestNumber;
}

var result = selectSmallestNumber();
console.log(result);

In this code, the selectSmallestNumber function initializes an empty array numbers. It then generates three random numbers between 1 and 100 using Math.random() and Math.floor() functions and adds them to the numbers array using the push method.

To find the smallest number in the array, the Math.min() function is used along with the spread operator ... to spread the numbers from the array as individual arguments.

Finally, the smallest number is returned from the function and printed to the console using console.log().

When you run this code, it will select three random numbers and output the smallest one.

huangapple
  • 本文由 发表于 2023年5月25日 10:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定