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

时间:2018-10-18 11:55:11

标签: javascript loops boolean

任务:

找到所提供参数的最小公倍数,它们可以被这两个参数以及这些参数之间范围内的所有序号均分。

代码检查所有元素是否都相同 最小的完美除数,并得出所有元素 返回true。问题在于它不会停止迭代。那里 是一个iterate boolean,最后变成false,但随后 在开始时,它再次被分配为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]));

3 个答案:

答案 0 :(得分:0)

最后,代码起作用了!

    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]));

答案 1 :(得分:0)

预览解决方案效率不高。

  
    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]));

答案 2 :(得分:0)

这是一个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]));