3

ユーザー プロンプトとして 2 つの変数を使用するプログラムを作成しています。最初のものは開始番号で、次は範囲を決定する番号です。次にプログラムは、collat​​z 関数を使用して、入力された元の数値がゼロになるまでに必要なステップ数を出力し、この情報を画面に表示します。次に、開始番号を 1 増やし、開始番号が 2 番目に入力された番号に達するまでプロセスを繰り返す必要があります。

現在、これは元の開始番号である 1 つの番号に対してのみ機能しています。開始番号 (最初の入力) と範囲番号 (2 番目の入力) の間のすべての数値を実行するように collat​​z 関数をコーディングする方法がわかりません。私の推測では、何らかの for ループが必要で、値の配列をプッシュします。 (数値が 0 に達するまでにかかるステップ) 別の配列内の独自の配列として (プログラムが実行したすべての数値のすべてのステップを保持します)

誰かがこれを手伝ってくれるなら、本当に感謝します、ありがとう

var numArray = [];

function getStartNum(){

   startNum = parseInt(prompt('Please enter a starting number greater than 0.'));

   if(!isPosNum(startNum)){
      alert("error! That is an incorrect value. Please renter an appropriate positive value.");
      getStartNum();
   } else {
    numArray.push(startNum);
    getRangeNum();
   }

}

function getRangeNum(){

   rangeNum = parseInt(prompt('Please enter a range value greater than 0.'));

   if(!isPosNum(rangeNum)){
      alert("error! That is an incorrect value. Please renter an appropriate positive value.");
      getRangeNum();
   } else {
    collatz();
   }

}

function isPosNum( number ) {

    if (isNaN( number )) {
        return false;
    } else if (number < 0) {
        return false;
    } else if (number == 0) {
        return false;
    } else {
        return true;
    }

}



function collatz() {

        //sets x to always be the value of the last element in the array
        x = numArray[numArray.length-1];

        //Sets y to the remainder of x
        y = x % 2;

        //if the value of y of 0, then the number is even and these calculations will be performed
        if (x == 1) {
            console.log('X has reached a value of 1');
            createBar();
        } else if ( y == 0) {
            console.log('Number is even.');
            z = x/2;
            console.log('Value of z is equal to ' + z);
            numArray.push(z);
            console.log(numArray);
            collatz();
        } else if ( y !== 0) {
            //If y is not equal to 0 then it is odd, and these calculations will be performed
            console.log('Number is odd.');
            z = (3 * x) + 1;
            console.log('Value of z is equal to ' + z);
            numArray.push(z);
            console.log(numArray);
            collatz();
        }

}

function maxValueIndex() {



}

function createBar() {

    var steps = numArray.length-1;
    console.log('Number of steps taken to get to 1 is: ' + steps);

    var p = document.createElement("p");
    document.body.appendChild(p); 
    var first = numArray[0]; 
    var output = document.getElementsByTagName("p")[0];
    output.innerHTML = 'Number of steps taken for ' + first + ' to reach 1 is: ' + steps;

    var g = document.createElement("div"); 
    g.id = "graph";
    document.body.appendChild(g); 

    for ( var i=0, n=numArray.length-1; i < n; i++) { 

        var line = document.createElement("p");
        line.className = "line";
        line.innerHTML = "|"; 
        document.body.appendChild(line);


    }

}




getStartNum();
4

1 に答える 1

2

startNum と startNum+rangeNum の間を繰り返す関数を追加するだけです。この反復では、startNum をインクリメントするたびに numArray を再起動し、collat​​z 関数を呼び出してすべてのステップを計算できます。

実行中のコードの変更を確認できます。

https://jsfiddle.net/kqcebswv/

これがお役に立てば幸いです。

配列の配列を生成してこれを行う必要がある場合は、次のようにすることができます。

collatzArray = [];
for (var i = startNum; i <= startNum + rangeNum; i++) {
    collatzArray.push([i]);
}

次に、collat​​zArray を反復する各 numArray にアクセスできます。

for (var i = 0; i < collatzArray.length; i++) {
    numArray = collatzArray[i];
    collatz();
}

しかし、このソリューションはより複雑だと思います。

于 2015-11-15T11:41:34.480 に答える