3

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!

0

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm old school - I always forget about that. I wanted to ensure that there were predictable numerical indexes on the values, since the original question included that idea.
how do i turn the array into a variable though
An array is a collection of variables. Re-read the end of my answer, it shows how to access the individual values stored in the array.
how do i make the program automatically access the values of the array
Read up on loops - I used one to set the values in my example. You can also use one to use all the values in the array.
2

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

Comments

0

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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.