so i need an answer to this
i was expecting to have the console look like this (if you input 69)
69
208
104
52
26
13
40
20
10
5
16
8
4
2
1
but it has nothing in the console so here is the program code
function mod(n, m) {
return ((n % m) + m) % m;
}
var a = prompt("please input a number");
do {
var b = mod(a, 2)
if (b == 0) {
var a = a / 2
} else a = (3 * a) + 1
console.log("a");
}
while (a !== 0)
There are several mistakes in this code.
First of all, you should write console.log(a) to log the a variable.
Also, you do not have to declare a variable again.
Lastly, your code is on an infinite loop because a doesn't match a !== 0 in the loop.
I believe this is the code you want.
function mod(n, m) {
return ((n % m) + m) % m;
}
let a = prompt("please input a number");
let b;
do {
b = mod(a, 2)
if (b == 0) {
a = a / 2
} else {
a = (3 * a) + 1
}
console.log(a);
}
while (a !== 1)
You can try this:
function mod(n, m) {
return ((n % m) + m) % m;
}
var a = prompt("please input a number");
do {
var b = mod(a, 2)
if (b == 0) {
var a = a / 2
} else a = (3 * a) + 1
console.log(a);
}
while (a !== 1)
And if you want to display your input as well, try:
function mod(n, m) {
return ((n % m) + m) % m;
}
var a = prompt("please input a number");
var c= a;
console.log(c);
do {
var b = mod(a, 2)
if (b == 0) {
var a = a / 2
} else a = (3 * a) + 1
console.log(a);
}
while (a !== 1)