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