Javascript:用布尔值停止从外部迭代循环



>任务:

找到所提供参数的最小公倍数,这些参数可以由两者以及这些参数之间范围内的所有序列号平均除以。

代码检查是否所有元素都具有相同的元素 最小的完美除数,它来到一个点,所有元素 返回 true。问题是它不会停止迭代。那里 是一个iterate boolean,最后变成假,但随后 一开始,它再次被分配给 true。有没有办法解决 那?还是有另一种方法可以做到这一点?

下面的代码设置为迭代 8 次。这就是它的重点 应该停止。如果设置为 9,它将继续。

当问题得到解决时,硬编码的 for 循环将被更改while (iteration) ,如果可以的话?

function smallestCommons(arr) {
let newArr = arr.sort();
// get the numbers between the two elements
let numbers = [];
let secondElement = arr[1];
for (let i = 0; i < secondElement; i++) {
numbers.push(newArr[1]--);
}
console.log(numbers);
// find the smallest common perfect divisor
function findCommon(array) {
let newArray = [...array]
let multiplier = newArray[0];
let product = 0;
let iterate = true;
// multiply the first element with multiplier
// and store the product
for (let i = 0; i < 8; i++) {
if (iterate) {
product = newArray[0] * multiplier++;
console.log('product', product);
console.log('multiplier', multiplier);
}
}
// check which elements of the
// array have a perfect division
// with the product
// and push the boolean results in a array,
let booleans = [];
for (j = 0; j < newArray.length; j++) {
booleans.push(product % newArray[j] === 0);
}
// count the elements that are true
let trueValues = 0;
for (let k = 0; k < booleans.length; k++) {
if (booleans[k] === true) {
trueValues++
}
}
console.log(booleans);
console.log('trueValues', trueValues);
console.log('iterate', iterate);
// if all elements are true, stop iteration.
if (trueValues === newArray.length) {
iterate = false;
}
console.log('iterate', iterate);
return product;
}
let result = findCommon(numbers);
return result;
}
console.log('result', smallestCommons([1, 5]));

代码终于可以工作了!

function smallestCommons(arr) {
let newArr = arr.sort((a, b) => a - b);
// get the numbers between the two elements
let numbers = [];
let firstElement = newArr[0]
let secondElement = newArr[1];
for (let i = 0; i < secondElement; i++) {
while (firstElement <= secondElement) {
numbers.push(secondElement--)
}
}

// find the smallest common perfect divisor
function findCommon(array) {
let newArray = [...array]
let multiplier = newArray[0];
let product = 0;
let booleans = [];
for (let i = 0; i < newArray.length; i++) {
booleans.push(false)
}
// Multiply the first element with multiplier
// and store the product.
// Check the product with each value from the
// newArray, for a perfect division.
// Increment the multiplier and check again.
// In every iteration remover the first value from
// the newArray and add the result of the
// new check at the end (FIFO).
// If all values are true break the booleans loop
// If all values are true break the outer loop.
for (;;) {
product = newArray[0] * multiplier;
//  console.log('product', product);
//  console.log('multiplier', multiplier);
for (let i = 0; i < newArray.length; i++) {
//  console.log('newArray', newArray[i]);
//  console.log('1', booleans);
booleans.shift()
booleans.push(product % newArray[i] === 0);
//console.log(booleans);
let pass = booleans.every(x => x === true)
//  console.log('pass', pass);
if (pass) {
break;
}
//    console.log("booleans", booleans);
}
let pass2 = booleans.every(x => x === true)
if (pass2) {
break;
}
//  console.log('2', pass2);
multiplier++
}
return product;
}
return findCommon(numbers);;
}
console.log('result', smallestCommons([23, 18]));

预览解决方案效率不高。


function smallestCommons(arr) {
arr.sort((a, b) => b - a);
// get the numbers between the two elements
let inBetweenNums = [];
for (let i = arr[0]; i >= arr[1]; i--) {
inBetweenNums.push(i)
}
// find the smallest common perfect divisor
let multiplier = 2;
let product = 0;
let dividesCleanly = true;
// Multiply the first two numbers with a multiplier.
// Check if the product divides perfectly all the numbers
// If there is a number that doesn't divide perfectly
// break the loop. So after break, dividesCleanly = true, which
// is the else, doesn't execute. The dividesCleanly is false
// so the product does not get returned. So we go to
// decrement multiplier, and so on.
// While there is a number that doesn't divide perfectly,
// the loop will break and the product will never be returned.
while (true) {
product = inBetweenNums[0] * inBetweenNums[1] * multiplier;
//console.log('prod... ', product);
for (let i = 0; i < inBetweenNums.length; i++) {
// console.log('inBe...1 ', inBetweenNums[i]);
if (product % inBetweenNums[i] !== 0) {
// console.log(inBetweenNums[i]);
dividesCleanly = false;
break;
}
dividesCleanly = true;
// console.log(dividesCleanly);
}
if (dividesCleanly) {
// console.log(product);
return product
} else {
multiplier++
}
}
}
console.log('result', smallestCommons([23, 18]));

这个是用do while循环而不是while(true(

function smallestCommons(arr) {
arr.sort((a, b) => b - a);
// get the numbers between the two elements
let inBetweenNums = [];
for (let i = arr[0]; i >= arr[1]; i--) {
inBetweenNums.push(i)
}
// find the smallest common perfect divisor
let multiplier = 2;
let product = 0;
let i = 0;
do {
product = inBetweenNums[0] * inBetweenNums[1] * multiplier;
//  console.log('prod... ', product);
for (i = 0; i < inBetweenNums.length; i++) {
//console.log('1.. ', i);
if (product % inBetweenNums[i] !== 0) {
//  console.log(inBetweenNums[i]);
break;
}
}
multiplier++
//console.log('1.. ', i);
} while (i !== inBetweenNums.length)
//  console.log(product);
return product
}
console.log('result', smallestCommons([1, 5]));

最新更新