My goal is to create a program that checks whether the user input is a perfect number or not. It has validation for the numbers entered. If the input IS a perfect number, I’d like to print out each of the divisors. I tried using this method:
{ for(int number=2; number <= 10000 ; number++) perfect(number); return 0; } void perfect(int number) { int total = 0; for (int i = 1; i < number; i++) { if (number % i == 0) total += i; } if (number == total) { for (int x = 1; x < number; x++) { if (number % x == 0) cout << x << " + "; } cout << " = " << number << endl; } }
However, I was unable to get the desired effect. I am very new to javascript and am struggling with inserting code in the correct way. Does anyone have a suggestion for how I can get the desired effect? Here is the code I have already written:
function check_prime() { var input = document.getElementById("enteredNumber").value; var number = parseInt(input); if (isNaN(number)) { alert("Oops! Please enter a valid number."); document.getElementById("enteredNumber").value=""; document.getElementById("result").innerHTML = ""; document.getElementById("enteredNumber").focus(); } else if (input.length === 0) { alert("Please enter a number."); document.getElementById("enteredNumber").focus(); } else if (!isNaN(number)) { if (is_perfect(number)) { document.getElementById("answer").innerHTML = "Congratulations! " + number + " is a perfect number." ; } else { document.getElementById("answer").innerHTML = "I'm sorry. " + number + " is not a perfect number. Try Again."; } } else { document.getElementById("answer").innerHTML = "Please enter a number."; } } function is_perfect(number) { var temp = 0; for(var i=1;i<=number/2;i++) { if(number%i === 0) { temp += i; } } if(temp === number) { return true; } else { return false; } } function clear_textbox(){ document.getElementById("answer").innerHTML = ""; document.getElementById("enteredNumber").value=""; document.getElementById("enteredNumber").focus(); }
Advertisement
Answer
I’d suggest revising your is_perfect()
function to return an array of divisors if the number is perfect and null
if the number is not perfect. Then the calling code has the divisors available for display when the input is a perfect number.
function is_perfect(number) { var temp = 0; var divisors = []; for(var i=1;i<=number/2;i++) { if (number%i === 0) { divisors.push(i); temp += i; } } return temp === number ? divisors : null; }
Then:
var divisors = is_perfect(number); if (divisors) { document.getElementById("answer").innerHTML = "Congratulations! " + number + " is a perfect number."; // display the divisors somewhere; the alert is just for show alert("Divisors: " + divisors.toString()); } else { ... }
[Note: In an earlier version of this answer, I had initialized temp
to 1 and divisors
to [1]
and had started the loop at 2, on the theory that 1 is always a divisor. Unfortunately, that’s wrong, since 1 is not a proper divisor of 1. The revised version of is_perfect()
now returns null
for an argument of 1 instead of [1]
. An alternative fix would have been to test explicitly for the case number === 1
, but that’s uglier (if perhaps a tiny bit more efficient, since it avoids one %
evaluation).]