0

Here's my code:

var myArray = new Array();
for (var i = 0; i < 24; i++ ){
            myArray.push([i]);
        }

How can I add/replace null values where there's 1, 3, 5, 7, 9...... ?

4
  • you want prime values as null or odd number values? Commented Feb 26, 2015 at 8:01
  • it should be like: 0, null, 2, null, 4, null, 6, null, 8.......... Commented Feb 26, 2015 at 8:05
  • ok all odd values as null Commented Feb 26, 2015 at 8:06
  • 1
    Split your issue in little part : first you need a function which tell if a number in parameter is a prime number. Then, use this function to replace by null value the prime number and you are done. Edit : plus, I quickly took a look at mathsisfun.com/prime_numbers.html and it seems not to be what you say. Did you mistake with odd and even numbers ? Commented Feb 26, 2015 at 8:07

2 Answers 2

2

Use this

for (var i = 0; i < 24; i++ ){
     if(i%2===1){
          myArray.push(null);
     else{
            myArray.push([i]);
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

For prime numbers and null:

var arr = [], i, j, primeNum = [];
for (i = 2; i <= 24; ++i) {
    if (!arr[i]) {
        primeNum.push(i);
        for (j = i << 1; j <= 24; j += i) {
            arr[j] = true;
        }
    } else {
        primeNum.push(null);
    }
}
console.log(primeNum)

If you just want the odd numbers as null:

var arr = [];
for (var i = 0; i < 24; i++ ){
    if( i % 2 === 1){
        arr.push(null);
    } else {
        arr.push(i);
    }
}
console.log(arr)

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.