未捕获的类型错误:减少没有初始值的空数组
我正在尝试设计这个非常简单的应用程序,您输入 2 个数字,应用程序将计算 2 个输入数字之间的所有数字的较小公倍数。
该函数本身运行良好(从控制台的结果来看);然而,当我尝试使用事件侦听器链接到 HTML 时,会发生故障。因此我假设在链接到 HTML 时我犯了一些错误。
对于某些数字,我的应用程序工作正常,但对于某些数字,由于某种原因,它不起作用(例如,输入 2 和 10、7 和 11...等)。错误说:
Uncaught TypeError: Reduce of empty array with no initial value
这是我的代码。
const smallNum = document.querySelector('#smallernum')
const bigNum = document.querySelector('#biggernum')
const button = document.querySelector('button')
const result = document.querySelector('#result')
button.addEventListener('click', () => {
result.value = smallestCommons([smallNum.value, bigNum.value])
})
function smallestCommons(arr) {
arr.sort((a,b) => a-b)
let lowerNum = arr[0]
let higherNum = arr[1]
let newArr = []
let primeOfArr = []
let shortDivisionArr = [];
// Create an array of nums inbetween the two extreme nums
for(let i = lowerNum; i <= higherNum; i++) {
newArr.push(i)
}
// Create an array of primes up until largest num
for(let i = 1; i <= higherNum; i++) {
let flag = 0;
for(let j = 2; j < i; j++) {
if(i % j === 0) {
flag = 1;
break;
}
}
if(i > 1 && flag === 0) {
primeOfArr.push(i)
}
}
// Short Division
for(let i = 0; i < primeOfArr.length; i++) {
while(newArr.some((num) => num % primeOfArr[i] === 0)) {
for(let j = 0; j < newArr.length; j++) {
if(newArr[j] % primeOfArr[i] === 0) {
newArr[j] = newArr[j] / primeOfArr[i]
}
} shortDivisionArr.push(primeOfArr[i])
}
}
return shortDivisionArr.reduce((a,b) => a*b);
}
<div class="container">
<div id="main">
<div class="title">
<h2>This App Helps You Find The Smallest Common Multiple Of Consecutive Numbers Between 2 Nums!</h3>
<p>2 nums inclusive. App for demonstration purpose. Please don't use this to cheat on your homework!</p>
</div>
<div class="calcs">
<div class="inputnums">
<input id="smallernum" type="text" placeholder="Enter a smaller num">
<input id="biggernum" type="text" placeholder="Enter a bigger num">
</div>
<button>Click to calculate!</button>
<input id="result" type="text" placeholder="Result is here!">
</div>
</div>
</div>
我做错了什么以及如何解决这个问题?
I'm trying to design this very simple application where you input 2 numbers and the app will calculate the smaller common multiple for all numbers between the 2 input numbers.
The function itself works well (judging from the result from console); however when I try to link to HTML with event listeners, glitches happen. Thus I'm assuming I'm making some mistakes when linking to HTML.
With some numbers my app works fine, but with some numbers, for some reason, it doesn't work (for instance, inputs 2 and 10, 7 and 11....etc.). The error says:
Uncaught TypeError: Reduce of empty array with no initial value
Here's my code.
const smallNum = document.querySelector('#smallernum')
const bigNum = document.querySelector('#biggernum')
const button = document.querySelector('button')
const result = document.querySelector('#result')
button.addEventListener('click', () => {
result.value = smallestCommons([smallNum.value, bigNum.value])
})
function smallestCommons(arr) {
arr.sort((a,b) => a-b)
let lowerNum = arr[0]
let higherNum = arr[1]
let newArr = []
let primeOfArr = []
let shortDivisionArr = [];
// Create an array of nums inbetween the two extreme nums
for(let i = lowerNum; i <= higherNum; i++) {
newArr.push(i)
}
// Create an array of primes up until largest num
for(let i = 1; i <= higherNum; i++) {
let flag = 0;
for(let j = 2; j < i; j++) {
if(i % j === 0) {
flag = 1;
break;
}
}
if(i > 1 && flag === 0) {
primeOfArr.push(i)
}
}
// Short Division
for(let i = 0; i < primeOfArr.length; i++) {
while(newArr.some((num) => num % primeOfArr[i] === 0)) {
for(let j = 0; j < newArr.length; j++) {
if(newArr[j] % primeOfArr[i] === 0) {
newArr[j] = newArr[j] / primeOfArr[i]
}
} shortDivisionArr.push(primeOfArr[i])
}
}
return shortDivisionArr.reduce((a,b) => a*b);
}
<div class="container">
<div id="main">
<div class="title">
<h2>This App Helps You Find The Smallest Common Multiple Of Consecutive Numbers Between 2 Nums!</h3>
<p>2 nums inclusive. App for demonstration purpose. Please don't use this to cheat on your homework!</p>
</div>
<div class="calcs">
<div class="inputnums">
<input id="smallernum" type="text" placeholder="Enter a smaller num">
<input id="biggernum" type="text" placeholder="Enter a bigger num">
</div>
<button>Click to calculate!</button>
<input id="result" type="text" placeholder="Result is here!">
</div>
</div>
</div>
What am I doing wrong and how can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。
传递到函数中的值(smallNum.value 和 bigNum.value)是字符串。
使用 parseInt() 将它们转换为整数,现在可以工作了。
Problem solved.
The values passed into the function (smallNum.value and bigNum.value) are strings.
Transformed them into integers using parseInt() and it's working now.