2017-09-08 9 views
1

私は、100になるまで強力な数値をすべて格納するようにしようとしています(数字は威力として表現するか、より正式にはhttps://en.wikipedia.org/wiki/Powerful_numberです)、それをlog(Store)します。JavaScript:ループ内でこれらのオブジェクトに対してプッシュが機能しないのはなぜですか?

私は2つのアレイを作成しました.1つは、そこに到着するのに使用された電力の説明と、ただ1つの番号です。

コードは、1000の平方根が〜31.6であるため、底辺が32より大きくないと仮定することから始まります。次に、別のループが形成され、指数が決定される。 10の2から10は1000より大きいので、最大の指数は10未満でなければなりません。

アルゴリズムが機能し、重複を削除できました。

numArrを印刷しようとしましたが、問題なく動作しました。 1000以下の強力な数字の40個全てが保存されていました。 しかし、結果の配列であるobjArrを印刷しようとすると、配列は、ループの最後の反復で、1000,31の2乗未満の50個のオブジェクトに過ぎません。

これはなぜですか?

var objArr = []; 
var numArr = []; 
var tempNum = 0; 
var tempObj = { 
    number: 0, 
    power: "" 
}; 

function shouldAppend(number) { 
    if (number > 1000) { 
    return false 
    } 
    return true; 
} 


// the base of the power 
for (var b = 2; b <= 32; b++) { 
    // the exponent of the power 
    for (var y = 2; y <= 10; y++) { 
    // tempNum is the result of the base raised to the exponent 
    tempNum = Math.pow(b, y); 
    // checks if "tempNum" is less than 1000 
    if (shouldAppend(tempNum)) { 
     // all of the below only exceutes if the tempNum is less or equal to 1000 
     // sets the temporary object's number property to the temporary number 
     tempObj.number = tempNum; 
     // sets the temporary object's power property to the current iterators 
     tempObj.power = `${b} to the power of ${y}`; 
     // logs the temporary object 
     console.log(tempObj); 
     // pushes the temporary object to the result array 
     objArr.push(tempObj); 
     numArr.push(tempNum); 
     // logs the current result array 
     console.log(objArr); 
    } 
    } 
} 
+0

いつも同じtempObjを使用していますか? –

+0

同じオブジェクトを何度も何度も変更しています。オブジェクトを配列にプッシュすると新しいオブジェクトは作成されません –

+0

コード内にはただ1つの 'tempObj'しかありません。 'objArr.push(tempObj)'はオブジェクトをコピーするのではなく、その単一のオブジェクトへの参照を格納するだけです。必要に応じてループ内のリテラルから新しいオブジェクトを作成します。 – Bergi

答えて

1

繰り返しごとに同じオブジェクトをプッシュしています。

objArr.push(Object.assign({}, tempObj)); 
0

をあなたは新しいオブジェクトの各ループの繰り返しを作成する必要があります。

がプッシュする前に、ループ内のオブジェクトのクローンを作成してみます。ループ内でtempObjをインスタンス化するだけで済みます。このコードはうまくいくはずです。

 var objArr = []; 
     var numArr = []; 
     var tempNum = 0; 

     function shouldAppend(number) { 
      if (number > 1000) { 
      return false 
      } 
      return true; 
     } 
     // the base of the power 
     for (var b = 2; b <= 32; b++) { 
      // the exponent of the power 
      for (var y = 2; y <= 10; y++) { 
      var tempObj = { 
       number: 0, 
       power: "" 
      }; 
      // tempNum is the result of the base raised to the exponent 
      tempNum = Math.pow(b, y); 
      // checks if "tempNum" is less than 1000 
      if (shouldAppend(tempNum)) { 
       // all of the below only exceutes if the tempNum is less or equal to 1000 
       // sets the temporary object's number property to the temporary number 
       tempObj.number = tempNum; 
       // sets the temporary object's power property to the current iterators 
       tempObj.power = `${b} to the power of ${y}`; 
       // logs the temporary object 
       console.log(tempObj); 
       // pushes the temporary object to the result array 
       objArr.push(tempObj); 
       numArr.push(tempNum); 
       // logs the current result array 
       console.log(objArr); 
      } 
      } 
     } 
関連する問題