Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

javascript - 代码生成器检查9个数字字符(ReactJS)(code generator checks 9 numerical characters (ReactJS))

I want an input field that validate a promotion code of 9 numbers:

(我想要一个输入字段来验证9个数字的促销代码:)

  • Nine numerical characters

    (九个数字字符)

  • When multiplying the first number by 9, the second by 8, the third by 7, and so on... the resulting number should be divisible by 11 and a single digit may not appear more than twice.

    (当将第一个数字乘以9,第二个数字乘以8,第三个数字乘以7,依此类推...所得的数字应被11整除,并且单个数字出现的次数不得超过两次。)

  • 613884922 is valid, because 6x9 + 1x8 + 3x7 + 8x6 + 8x5 + 4x4 + 9x3 + 2x2 + 2x1 = 220 / 11 = 20 (whole number, no digit is repeated 3+ times)

    (613884922有效,因为6x9 + 1x8 + 3x7 + 8x6 + 8x5 + 4x4 + 9x3 + 2x2 + 2x1 = 220/11 = 20(整数,无数字重复3+次))

  • 538820102 is invalid, because 5x9 + 3x8 + 8x7 + 8x6 + 2x5 + 0x4 + 1x3 + 0x2 + 2x1 = 188 / 11 = 17.09 (not a whole number)

    (538820102无效,因为5x9 + 3x8 + 8x7 + 8x6 + 2x5 + 0x4 + 1x3 + 0x2 + 2x1 = 188/11 = 17.09(不是整数))

  • 131888331 is invalid (digits 1, 3 and 8 appear too often)

    (131888331无效(数字1、3和8经常出现))

 const App = () => { const multiply = [9, 8, 7, 6, 5, 4, 3, 2, 1]; const handleSubmit = (e) => { const promotionCode = [6,1,3,8,8,4,9,2,2]; if(e) e.preventDefault(); const [input] = e.target.children console.log("valid or invalid"); } return ( <form onSubmit={handleSubmit}> <input placeholder="Promotion Code" type="number"/> <button>Submit!</button> </form> ) }export default App; 

  ask by Fresh developer translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Any time you're struggling to wrap your head around large tasks like these I recommend breaking your problem down into individual parts and turning them into functions you can tackle one by one.

(任何时候,当您为解决诸如此类的大型任务而挣扎时,我建议将您的问题分解为各个部分,并将其转变为可以一一解决的功能。)

First you probably want to make sure that no number is repeated more than twice.

(首先,您可能要确保没有数字重复超过两次。)

A good way to do this would be to tally up how often each number is repeated with something like this.

(做到这一点的一个好方法是计算出每个数字重复这样的频率。)

const hasValidFrequency = (string) => {
  const array = string.split("")
  const tallied = {}
  for (const elem of array) {
    tallied[elem] = (tallied[elem] || 0) + 1
  }
  // you might have to tweak this function slightly to make it
  // return the problematic digit if necessary
  return Object.values(tallied).every(frequency => frequency < 3)
}

This way you could call the function and make sure every key in here has a maximum value of 2.

(这样,您可以调用该函数并确保此处的每个键的最大值为2。)

For the next validation, you could do a for loop backwards and figure start multiplying the digits.

(对于下一个验证,您可以向后进行for循环,然后数字开始乘以数字。)

const isValidMultiple = (string) => {
  let result = 0
  for (let i = 9; i >= 1; i--) {
    const letter = string[string.length - i]
    result += Number(letter) * i
  }
  return result % 11 === 0
}

Now you can put these together in your handler and call it in a readable manner.

(现在,您可以将它们放到处理程序中,并以可读的方式调用它。)

if (hasValidFrequency(input) && isValidMultiple(input)) {
  submitVerifiedInput(input)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...