function main(){ var n = parseInt(prompt('What should the number be?')); alert('The given number ' + n + ' is ' + (isPrime(n) ? '' : ' not') + ' a prime.'); } function isPrime(n) { var counter = n - 1; var b = n / counter; var c = Math.floor(b); if (n < 1) { return false; } if (n === 1) { return false; } else { if (b - c != 0) { } } } var n = parseInt(prompt('What should the number be?')); alert('The given number ' + n + ' is ' + (isPrime(n) ? '' : ' not') + ' a prime.'); function main(){ var n = parseInt(prompt('What should the number be?')); alert('The given number ' + n + ' is ' + (isPrime(n) ? '' : ' not') + ' a prime.'); } function isPrime(n) { var counter = n - 1; var b = n / counter; var c = Math.floor(b); if (n < 1) { return false; } if (n === 1) { return false; } else { if (b - c != 0) { } } } function isPrime(n) { var counter = n - 1; var b = n / counter; var c = Math.floor(b); if (n < 1) { return false; } if (n === 1) { return false; } else { if (b - c != 0) { } } } var n = parseInt(prompt('What should the number be?')); alert('The given number ' + n + ' is ' + (isPrime(n) ? '' : ' not') + ' a prime.'); I don't think your problems is with too many variables. You should use functions to separate code parts with different purpose:
function main(){ var n = parseInt(prompt('What should the number be?')); alert('The given number ' + n + ' is ' + (isPrime(n) ? '' : ' not') + ' a prime.'); } function isPrime(n) { var counter = n - 1; var b = n / counter; var c = Math.floor(b); if (n < 1) { return false; } if (n === 1) { return false; } else { if (b - c != 0) { } } } or
function PrimeChecker(){ var n = this.readNumber(); this.displayResult(n, this.isPrime(n)); } PrimeChecker.prototype = { readNumber: function (){ return parseInt(prompt('What should the number be?')); }, isPrime: function (n) { var counter = n - 1; var b = n / counter; var c = Math.floor(b); if (n < 1) { return false; } if (n === 1) { return false; } else { if (b - c != 0) { } } }, displayResult: function (n, isPrime){ alert('The given number ' + n + ' is ' + (isPrime ? '' : ' not') + ' a prime.'); } }; new PrimeChecker(); If you want to group variables you can still use an object to store them:
var map = { a: 1, b: 2, c: 3 }; map.d = 4; map.e = 5; alert(map.e); If you generate variables without name, you can use an array:
var array= [ "a", "b", "c" ]; array.push("d"); array.push("e"); alert(array[4]); lang-js