2

I am creating a program which takes two variables as user prompts. The first one is a starting number, the next is a number to determine a range. The program then spits out how many steps it takes for the original number entered to reach zero using the collatz function and then displays this information to the screen. It should then increment the starting number by 1 and repeat the process until starting number reaches the second inputted number

Currently i only have this working for one number, which is the original starting number. I am unsure of how to code the collatz function to run through all the numbers between the start number (first input) and the range number(second input) my guess is i will need some sort of for loop and push an array of the values (steps it takes for number to reach 0) as its own array within another array (which holds all the steps for all the numbers the program ran through)

if anyone can help with this i'd really appreciate it, thanks

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();
Kyle Bing
  • 253
  • 2
  • 6
  • 20

1 Answers1

2

You only need to add a function that iterate between startNum and startNum+rangeNum. In this iteration you can restart numArray with each increment of startNum and call the collatz function to calculate all the steps.

You can see the modification of your code running on:

https://jsfiddle.net/kqcebswv/

I hope this helps you.

If you need to do this generating an array of arrays you can do this:

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

then you can access each numArray iterating the collatzArray:

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

but I think this solution si more complex.

  • Hey! Thanks very much for your answer this works! One question however, another thing I am trying to achieve, is to output a different character for the array with the longest steps. [Refer to the picture in the question post and look at value #9]. I was leaning to dynamically generating arrays to do this because in order to see which array is the longest it needs to be compared to the other arrays. But can it be implemented in your js fiddle example even though the array resets each time? – Kyle Bing Nov 15 '15 at 11:57