From 20ddcc30b66c646e80c77dd538f1791a62f12c84 Mon Sep 17 00:00:00 2001 From: Jonathan Olson Date: Tue, 11 Dec 2018 12:13:24 -0700 Subject: [PATCH] Get rid of for-of loops in fractions-common, see https://github.com/phetsims/tasks/issues/972 --- js/common/EnumerationMap.js | 4 ++-- js/common/model/PrimeFactorization.js | 11 +++++++---- js/common/model/PrimeFactorizationTests.js | 8 ++++---- js/game/model/CollectionFinder.js | 11 ++++++----- js/game/model/FractionChallenge.js | 6 ++++-- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/js/common/EnumerationMap.js b/js/common/EnumerationMap.js index e31781e..2373cc4 100644 --- a/js/common/EnumerationMap.js +++ b/js/common/EnumerationMap.js @@ -23,10 +23,10 @@ define( require => { // @private {Enumeration} this._enumeration = enumeration; - for ( let entry of enumeration.VALUES ) { + enumeration.VALUES.forEach( entry => { assert && assert( this[ entry ] === undefined, 'Enumeration key override problem' ); this[ entry ] = factory( entry ); - } + } ); } /** diff --git a/js/common/model/PrimeFactorization.js b/js/common/model/PrimeFactorization.js index 63ff9bb..8062d97 100644 --- a/js/common/model/PrimeFactorization.js +++ b/js/common/model/PrimeFactorization.js @@ -92,12 +92,12 @@ define( require => { binaryOperation( factorization, operation ) { const primes = _.uniq( [ ...this.factors, ...factorization.factors ].map( f => f.prime ) ).sort( ( a, b ) => a - b ); const factors = []; - for ( let prime of primes ) { + primes.forEach( prime => { const order = operation( this.getOrder( prime ), factorization.getOrder( prime ) ); if ( order ) { factors.push( new PrimeFactor( prime, order ) ); } - } + } ); return new PrimeFactorization( factors ); } @@ -164,7 +164,8 @@ define( require => { * @returns {boolean} */ divides( factorization ) { - for ( let factor of this.factors ) { + for ( let i = 0; i < this.factors.length; i++ ) { + const factor = this.factors[ i ]; if ( factor.order > factorization.getOrder( factor.prime ) ) { return false; } @@ -236,7 +237,9 @@ define( require => { const factors = []; - for ( let prime of Primes.primes ) { + const primes = Primes.primes; + for ( let i = 0; i < primes.length; i++ ) { + const prime = primes[ i ]; // A prime that is a divisor (not equal to our number) would be less than the max divisor if ( prime > maxDivisor ) { break; } diff --git a/js/common/model/PrimeFactorizationTests.js b/js/common/model/PrimeFactorizationTests.js index e1f2fd7..2ddda9b 100644 --- a/js/common/model/PrimeFactorizationTests.js +++ b/js/common/model/PrimeFactorizationTests.js @@ -21,9 +21,9 @@ define( require => { QUnit.test( `Prime factorizations of everything up to ${MAX_NUMBER}`, assert => { for ( let n = 2; n < MAX_NUMBER; n++ ) { const factorization = PrimeFactorization.factor( n ); - for ( let factor of factorization.factors ) { + factorization.factors.forEach( factor => { assert.ok( Primes.isPrime( factor.prime ), `Prime of factorization of ${n} is prime` ); - } + } ); assert.equal( n, factorization.number, `Prime factorization of ${n} is consistent` ); } } ); @@ -62,9 +62,9 @@ define( require => { const factorization = PrimeFactorization.factor( a ); const divisors = factorization.divisors; - for ( let divisor of divisors ) { + divisors.forEach( divisor => { assert.equal( a / divisor.number, factorization.divided( divisor ).number, `Division of ${a} divided by ${divisor.number}` ); - } + } ); } } ); diff --git a/js/game/model/CollectionFinder.js b/js/game/model/CollectionFinder.js index 171957e..6c050e7 100644 --- a/js/game/model/CollectionFinder.js +++ b/js/game/model/CollectionFinder.js @@ -78,9 +78,9 @@ define( require => { const constraintIndex = findMinConstraintIndex(); const divisorDenominators = constraintDenominators[ constraintIndex ]; - for ( let uncomputedDenominator of filterUncomputedDenominators( divisorDenominators ) ) { + filterUncomputedDenominators( divisorDenominators ).forEach( uncomputedDenominator => { entries.push( new Entry( uncomputedDenominator, lcm.divided( uncomputedDenominator ) ) ); - } + } ); for ( let i = 0; i < constraintDivisors.length; i++ ) { const divisor = constraintDivisors[ i ]; const divisorDenominators = constraintDenominators[ i ]; @@ -95,9 +95,9 @@ define( require => { constraintDivisors.splice( constraintIndex, 1 ); constraintDenominators.splice( constraintIndex, 1 ); } - for ( let uncomputedDenominator of filterUncomputedDenominators( denominators ) ) { + filterUncomputedDenominators( denominators ).forEach( uncomputedDenominator => { entries.push( new Entry( uncomputedDenominator, lcm.divided( uncomputedDenominator ) ) ); - } + } ); // @private {Array.} this.entries = entries; @@ -182,7 +182,8 @@ define( require => { coefficients.push( coefficient ); let constraintsSatisfied = true; - for ( let constraint of entry.constraints ) { + for ( let i = 0; i < entry.constraints.length; i++ ) { + const constraint = entry.constraints[ i ]; if ( !constraint.satisfies( r, coefficients ) ) { constraintsSatisfied = false; break; diff --git a/js/game/model/FractionChallenge.js b/js/game/model/FractionChallenge.js index 13f8fbc..b670e98 100644 --- a/js/game/model/FractionChallenge.js +++ b/js/game/model/FractionChallenge.js @@ -295,7 +295,8 @@ define( require => { // If we already have one out, don't look for more if ( groupArray.length >= 2 ) { return; } - for ( let groupStack of stackArray ) { + for ( let i = 0; i < stackArray.length; i++ ) { + const groupStack = stackArray[ i ]; if ( !groupStack.isEmpty() ) { const group = groupStack.array.pop(); group.clear(); @@ -560,7 +561,8 @@ define( require => { const possibilities = fractionPossibilities[ i ]; - for ( let possibility of possibilities ) { + for ( let i = 0; i < possibilities.length; i++ ) { + const possibility = possibilities[ i ]; if ( currentCollection.contains( possibility.total ) ) { currentCollection = currentCollection.minus( possibility.total );