【问题标题】:How do I make Math.random pick the same amount of user input value from 4 different arrays?如何让 Math.random 从 4 个不同的数组中选择相同数量的用户输入值?
【发布时间】:2021-02-16 05:07:46
【问题描述】:

聪明的人晚上好!

我是编程新手,需要您的帮助!我被困在这个代码挑战中,希望能得到一些关于如何解决这个问题的意见。如何让 Math.random 从 4 个不同的数组中选择相同数量的用户输入值?我很确定我写的 math.random 是错误的。我撞到了一堵墙。提前感谢您的帮助!我认为这就是你的做法;通过将 4 个数组组合成一个数组,并以某种方式使 Math.random 方法选择用户在提示中输入的随机值。再次感谢您!

let array1 = [1, 2, 3];
let array2 = [A, B, C];
let array3 = [a, b, c];
let array4 = [%, @. ^];

let fourArray[array1, array2, array3, array4];

let dnumConfirm = confirm("Do you want numbers in your password?");
let capConfirm = confirm("Do you wantn capital letters in your password?");
let lowConfirm = confirm("Do you want lowercase letters in your password?");
let specialConfirm = confirm("Do you want special characters in your           password?");
let userInput = parseInt(prompt("How many capital letters would you like?"));
function random(userInput) {
 
  
if(dnumConfirm && capConfirm && lowConfirm && specialConfirm) {
     const randomValue = Math.floor(Math.random * Math.floor(userInput(fourArray))); 
     return randomValue; 
}

【问题讨论】:

  • 您能否澄清一下“从 4 个不同的数组中选择相同数量的用户输入值”是什么意思?
  • 所以,我提示并输入一个整数 14。我希望 Math.random 方法从 4 个数组中选择 14 个字符。所以我想你把这 4 个数组放在一个数组中。我希望这是有道理的。
  • 嘿,所以我添加了一个答案,你能检查一下这是不是你想要的吗?

标签: javascript arrays user-input


【解决方案1】:
let array1 = [1, 2, 3];
let array2 = ["A", "B", "C"];
let array3 = ["a", "b", "c"];
let array4 = ["%", "@". "^"];

let fourArray = [...array1, ...array2, ...array3, ...array4];

let dnumConfirm = confirm("Do you want numbers in your password?");
let capConfirm = confirm("Do you wantn capital letters in your password?");
let lowConfirm = confirm("Do you want lowercase letters in your password?");
let specialConfirm = confirm("Do you want special characters in your           password?");
let userInput = parseInt(prompt("How many capital letters would you like?"));

function random(userInput) {   
     if(dnumConfirm && capConfirm && lowConfirm && specialConfirm) {
          let randomValue = "";
          for (let i = 0; i < userInput; i++)
               randomValue += userInput[Math.floor(Math.random() * fourArray.length)]; 
          return randomValue; 
     }
}

将您的array2array3array4 转换为字符串。 通过扩展其他数组将fourArray 更改为一维数组。 我还做了一个循环,会在你的 random 函数中创建随机字符串。 这对你有用吗?

【讨论】:

    【解决方案2】:

    我们可以使用Math.randomMath.floor 使用以下公式生成一个从 0 到 N 的随机整数: Math.floor(Math.random() * (N + 1))

    首先创建一个空数组来保存其他数组。

    let charTypes = []; 
    

    当用户选择一种字符类型(小写字母等)时,通过 push 将相应的数组添加到 charTypes。 示例:charTypes.push(array3);

    将此数组的长度保存到一个变量中。

    let differentKinds = charTypes.length; 
    

    使用循环遍历字符总数userInput,并在 [0, differentKinds -1] 上选择一个随机整数。

    let password = ''; 
    for(let i = 0; i < userInput; i++){
        // get index on [0, differentKinds - 1]
        let j = Math.floor(Math.random() * (differentKinds));
        
        //Use the array charTypes[j] take a random index on [0,2]. 
        let k = Math.floor(Math.random() * 3);  
        
        // We can now concatenate a random character with charTypes[j][k]
        password += charTypes[j][k]; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多