JSFiddle - React, Tailwind, and code Playground

by kontrach

JavaScript

//console.time('time is:');
// Calculation primes using arrays
var i, j, k, n, m, length, undefLength, tempArr, withoutUndef, primes, currPrime, nextPrime;
currPrime = [];
nextPrime = [];
tempArr = [];
primes = [];
withoutUndef = [];
var range = prompt('Input the range', '');

for (i = 2; i <= range; i++) {
    tempArr[i - 2] = i;
}

for(j = 0; j < range; j++) {
    //delete not prime numbers
    length = tempArr.length;
    for (k = 0; k <= length; k++) {
        if ( tempArr[k] === undefined || (tempArr[k] % currPrime !== 0) ) {
            continue;
        }else{
            delete(tempArr[k]);
        }
    }
    /*
//---Method without deleteng undefined elements arrays */
    for (m = 0; m < tempArr.length; m++) {
        if (tempArr[m] === undefined) {
            continue;
        }else{
            currPrime = tempArr[m];
            primes.push(currPrime);
            break;
        }
    }/**/
    
/*---Method with deleting elements and rewriting arrays
    //Create new array without undefined elements 
    withoutUndef.length = 0;
    for (n = 0; n < length; n++) {
        if (tempArr[n] === undefined) {
            continue;
        }else{
            withoutUndef.push(tempArr[n]);
        }
    }
    //assign next prime and divider
    if (withoutUndef[0] === undefined){
        break;
    }
        currPrime = withoutUndef[0];
        primes.push(withoutUndef[0]);
   
    //delete undefined elements in tempArr
    undefLength = withoutUndef.length;
    tempArr.length = 0;
    for (m = 0; m < undefLength; m++) {
        tempArr[m] = withoutUndef[m];
---    }*/
}
alert('Primes are: ' + primes);
//console.timeEnd('time is:');