Reputation: 49
everyone! I am pretty new at Javascript and I wanted to challenge myself by making a program that will figure out if the number entered is prime. So far, this is my code:
var a = prompt('What should the number be?');
var n = parseInt(a);
var counter = n - 1;
var b = n/counter;
var c = Math.floor(b);
if (n < 1) {
window.alert('Invalid: negative numbers are neither prime nor composite!');
}
if (n === 1){
window.alert('1 is neither prime nor composite!');
} else {
if (b-c != 0) {
}
}
I am planning to make a bunch of variables, each with a name that differs by a number. I do not need an easier or different way to write my code. So how do I make the script write many different variable automatically? Thanks in advance!
Upvotes: 3
Views: 93
Reputation: 26129
I don't think your problems is with too many variables. You should use functions to separate code parts with different purpose:
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.');
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]);
Upvotes: 2
Reputation: 26380
This sounds like a good case for using an array. It's an easy way to store lots of values in the same variable name, but called by an index - a number. Check out the MDN page for arrays.
Here's an example of stashing lots of values in an array:
var bigNumber = 100000;
var myArray = new Array();
var tally = 0;
for( i = 0; i < bigNumber; i++ )
{
tally += i;
myArray[i] = tally;
}
When this is done, myArray will have 100000 values in it. myArray[0] = 0
, myArray[1] = 1
, myArray[2] = 3
, myArray[3] = 6
, myArray[4] = 10
, etc.
Upvotes: 5
Reputation: 11925
The evil way to do it would be to create variables on the global object (window), like so:
// This is bad because it pollutes the global namespace:
for (var i = 0; i < 10; i++) {
window['num' + i] = i;
}
A better way would be to create an empty object and use that as a namespace for your variables.
var o = {};
for (var i = 0; i < 10; i++) {
o['num' + i] = i;
}
Upvotes: 0