這可能是非常基本的,但我的代碼似乎有一些錯誤或期望可能對我不起作用。一些背景資訊:我的代碼是這樣創建的,它要求用戶輸入一個整數。然后將分析用戶的輸入,并向用戶顯示相乘后的數字輸出等于用戶的輸入。
期待
輸入:7 輸出(在控制臺中):(1,7)(7,1)。
但是相反,我的代碼還輸入了等于用戶輸入的小數。
現實
輸入:7 輸出:(1.1666666666666667, 6), (1.4, 5), (1.75, 4), (2.3333333333333335, 3), (3.5, 2), (7, 1), (Infinity, 0)。
var numInput= parseInt(prompt("Please enter a number larger than 1"));
//This asks the user for the input
var valueArray = [];//This is the empty array we are going to use in the further code.
if(numInput <= 0 || numInput <= 1) {
console.log("Goodbye!")
}//This line is an if loop which if the user inputs 0 or 1 then the code will end.
while(numInput > 0) {// This while loop is there so that the user can input as many numbers he wants
var valueArray = [];//Now the numbers are inside this empty array
var numInput = parseInt(prompt("Please enter a number larger than 1"));
for (var iterator = 0; iterator < numInput; iterator) {//This for loop is the calculation, for when a = 1, the a has a greater value then
var valueSubtracted = numInput / iterator //This is where the variable subtracts the orignal value n so that we have something along the lines of (1,6) instead (1,7)
valueArray.unshift(valueSubtracted ", " iterator ); //This just moves the answers into a concantination, and moves into the array
}
console.log("The additive combinations are: " "(" valueArray.join("), (") "). ");
}
我真正想要的只是洗掉小數點和與之關聯的數字。例如:
輸入:7 輸出:(1.1666666666666667, 6) , (1.4, 5), (1.75, 4), (2.3333333333333335, 3) , (3.5, 2), (7, 1), (Infinity, 0)。
注意上面:粗體和斜體應該從陣列中洗掉。 這就是它在控制臺中的樣子。
uj5u.com熱心網友回復:
您應該檢查它是否可以被整數整除。為此,請在-clause中使用n % m === 0并檢查它。if它應該在for-loop 內創建。另外:做零除法不會導致錯誤。相反,它回傳Infinity。1考慮在-loop 中將起始值更改為for以避免這種情況。
uj5u.com熱心網友回復:
由于我因友善而被“優雅地”否決,因此這里有一個更好的答案。
代碼:
while (true) {
const numInput = parseInt(prompt("Please enter a number larger than 1").replace(/\D/g, ""));
if (numInput > 1) {
const valueArray = [];
for (let iterator = 1; iterator <= numInput; iterator ) {
const valueDivided = ((numInput / iterator) % 1 === 0) ? (numInput / iterator) : false
if (valueDivided) valueArray.unshift(`(${valueDivided}, ${iterator})`);
}
console.log(`The additive combinations are: ${valueArray.join(",")}`)
continue;
}
console.log("Goodbye!")
}
分解:
1-我用了const而let不是var
2-無限回圈:
while (true) // will keep repeating the same process until user clicks "Cancel"
3-決議前清理
const numInput = parseInt(prompt("Please enter a number larger than 1").replace(/\D/g, ""));
// uses RegEx to remove any non-numeric values
// parseInt() to get integer from prompt text
if數字不大于 1console.log("Goodbye!")
4- used letinstead of varin the forloop and replace iteratorwith iterator ... 不同之處在于 iterator首先遞增 1 THEN可以使用變數。另外,為了回答你對我的第一個答案的第二條評論,它只記錄了 (1,7) 而不是 (7,1) 因為你有iterator < numInput所以回圈無法達到 7,它在 6 處停止。我改為iterator <= numInput改為。
5- 計算可能的劃分:
const valueDivided = ((numInput / iterator) % 1 === 0) ? (numInput / iterator) : false
// if the value is a normal number return it, else return false
6-
// only add to the array if truthy
if (valueDivided) valueArray.unshift(`(${valueDivided}, ${iterator})`)
// Normal numbers with the exception of 0 are truthy values, which means if(1) returns true... if(0) returns false
// I used template literals `${dynamic} text ${dynamic}`
// so any added value will be like: (1,7) for example
.join7- 然后使用您在原始帖子中使用的方式加入結果
8-continue告訴while回圈移動以繼續其作業。將其視為return功能。
uj5u.com熱心網友回復:
好吧,您的代碼中有很多需要改進/更正的地方,但我只會專注于使其按您希望的方式作業。只需更改以下行:
var valueSubtracted = ((numInput / iterator) % 1 === 0) ? (numInput / iterator) : Infinity
// check if valueSubtracted is an N group number or else set it as Infinity
if(isFinite(valueSubtracted)) valueArray.unshift(valueSubtracted ", " iterator )
// only add to array if finite number
另外,numInput<=1已經是nimInput<=0,為什么要兩者兼而有之?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537351.html
標籤:javascript数组
下一篇:如何將陣列中的奇數變為偶數?C
