简体   繁体   English

为什么我的外部变量在使用 while 循环时不会改变?

[英]Why does my outer variable not change when using a while loop?

function ranNum(value) {
  return Math.ceil(Math.random() * value)
}

function createRanId(value) {
  const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
  const numbers = '0123456789'.split('')
  const idLength = value || 6
  let id = ''

  for(let i = 0; i < idLength; i++) {
    const numOrAlpha = ranNum(2)

    numOrAlpha === 1 ? id += alphabet[ranNum(alphabet.length - 1)] : id += numbers[ranNum(numbers.length - 1)]
  }

  return id
}

function isAllNumbers(arr) {
  return arr.every(value => Number.isInteger(value))
}

function allNumberId() {
  let count = 0
  let ranNum = createRanId(2).split("");

  while(!isAllNumbers(ranNum)) {
    ranNum = createRanId(2).split("")
    count++
  } 


  return [count, ranNum]
}

console.log(allNumberId())

So what im doing is generating a random string that consists of numbers and letters (for example: 3e3jjf).所以我正在做的是生成一个由数字和字母组成的随机字符串(例如:3e3jjf)。 What i'm trying to achieve is to find a generated combination that only consists of numbers (for example: 235033).我想要实现的是找到一个仅由数字组成的生成组合(例如:235033)。 However, my code doesnt seem to work and ends up in an infinite loop.但是,我的代码似乎不起作用并最终陷入无限循环。 I'm making a thinking error somewhere in the function allNumberId我在 function allNumberId的某处犯了一个思维错误

edit: this is obviously not production code or anything.编辑:这显然不是生产代码或任何东西。 I'm just practicing some javascript.我只是在练习一些 javascript。 It bugs me that I cant find what I do wrong here.让我感到困扰的是,我在这里找不到我做错了什么。

In your code you are checking for a number在您的代码中,您正在检查一个数字

Number.isInteger("6")

When it is a string it is false.当它是一个字符串时,它是假的。 So you need to alter your code to try to make it into a number or other option is isNaN()因此,您需要更改代码以尝试将其变为数字或其他选项是isNaN()

 function ranNum(value) { return Math.ceil(Math.random() * value) } function createRanId(value) { const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('') const numbers = '0123456789'.split('') const idLength = value || 6 let id = '' for(let i = 0; i < idLength; i++) { const numOrAlpha = ranNum(2) numOrAlpha === 1? id += alphabet[ranNum(alphabet.length - 1)]: id += numbers[ranNum(numbers.length - 1)] } return id } function isAllNumbers(arr) { return arr.every(value => Number.isInteger(+value)) } function allNumberId() { let count = 0 let ranNum = createRanId(2).split(""); while(.isAllNumbers(ranNum)) { ranNum = createRanId(2),split("") count++ } return [count. ranNum] } console.log(allNumberId())

Your check could also be done as您的检查也可以作为

const isInvalid = yourString.split("").map(Number).some(isNaN)

The problem is the usage of Number.isInteger .问题是Number.isInteger的使用。 You're actually passing strings there (single-character strings consisting of a digit or a alphabet char), which is never a number (integer or not) so Number.isInteger always returns false and your isAllNumbers function doesn't recognice what it should.你实际上是在那里传递字符串(由数字或字母字符组成的单字符串),它永远不是数字(整数或非整数),所以Number.isInteger总是返回false并且你的isAllNumbers function 不知道它应该是什么.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM