Why isn't this simple javascript code working? - javascript

The code is supposed to check whether the first few triangular numbers are prime (they are not), but it does not run.
<!DOCTYPE HTML>
<html>
<head><title>C1E9P1</title>
<script>
for(i = 3; i < 13; i++){
prime = true;
n = (i*(i+1))/2;
for(i = 2; i < Math.sqrt(n)+1; i++){
if(n%i == 0){
prime = false;
}
}
if(prime){
document.write(n+" is prime.");
document.write("<br>");
} else {
document.write(n+" is composite.");
document.write("<br>");
}
}
</script>
</head>
</html>

You used same variable for your two for loops that are nested!
for(i = 3; i < 13; i++){ <-- i
for(i = 2; i < Math.sqrt(n)+1; i++){ <-- i

Try this and look at your JavaScript console :
for(var i = 3; i < 13; i++){
var prime = true;
var n = (i*(i+1))/2;
for(var j = 2; j < Math.sqrt(n)+1; j++){
if(n%j == 0){
prime = false;
}
}
if(prime){
console.log(n+" is prime.");
console.log("<br>");
} else {
console.log(n+" is composite.");
console.log("<br>");
}
}

I've modified script, but don't known if algorithm is correct.
for(var i = 3; i < 13; i++){
var prime = true;
var n = (i*(i+1))/2;
for(var j = 2; j < Math.sqrt(n)+1; j++){
if(n%j == 0){
prime = false;
}
}
if(prime){
document.write(n+" is prime.");
document.write("<br>");
} else {
document.write(n+" is composite.");
document.write("<br>");
}
}

You should change the inner for-loop's variable name to something else other than i.
for(i = 3; i < 13; i++){
prime = true;
n = (i*(i+1))/2;
for(j = 2; j < Math.sqrt(n)+1; j++){
if(n%j == 0){
prime = false;
}
}
You should be all good after that.

Related

My code code infinitely and crashes the page

This is my code:
// Population
var Gene = function(text){
if(text){
this.text = text;
}
};
Gene.fitness = 0;
Gene.generation = 0;
var word = new Gene('Hello');
// This is where it crashes!!
// Make elements
var genArr = [];
var population = 20;
var mutation = 0;
for(var i = 0; i < population; i++){
var gene = "";
var abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var j = 0; i < word.text.length; j++) {
var element = abc.charAt(Math.floor(Math.random() * abc.length));
gene += element;
}
genArr.push(gene);
}
// Divide them - fitness
// 1/20 - 0.05% each
var fitElements = [];
for (var i = 0; i < genArr.length; i++) {
var score = 0;
var curWord = Array.from(genArr[i]);
for (var j = 0; j < word.text.length; j++) {
if(genArr[j].substr(j, 1) == word.text.substr(j, 1)){
score += 1;
}
}
if(score > 0){
fitElements.push([genArr[i], (score * (1 / population)) ** 2]);
}
}
for (var i = 0; i < fitElements.length; i++) {
console.log('Element: ' + fitElements[i][0] + ', Score: ' + fitElements[i][1]);
}
My problem is that it crashes the page but doesn't gives errors. The idea is to create a simple word register in fitElements Array but I can't see what am I missing?
Thanks in advance.
In your code the nested for loop with variable j's end condition relies on i.
Take a the line:
// VVVV it relies on i instead of j
for (var j = 0; i < word.text.length; j++) {
var element = abc.charAt(Math.floor(Math.random() * abc.length));
gene += element;
}
The new code would look something like
for (var j = 0; j < word.text.length; j++)
var element = abc.charAt(Math.floor(Math.random() * abc.length));
gene += element;
}
The only difference in the entire sample is the i is changed to a j. Cheers!

Print prime numbers between 0 and 100

I'm trying to print all prime number between 0 and 100, but when executing this code the browser's tab just outputs nothing!!
for(var i = 2; i < 100; i++)
{
var prime = [];
for(var j = 0; j <= i; j++)
{
var p = i % j;
}
if(p != 0) prime.push(i);
else continue;
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}
Try this one. I have also optimise the code (you only need to check upto sqrt(i) ).
var prime = [];
prime.push(2); //smallest prime
var flag = 0;
for(var i = 3; i < 100; i=i+2) //skip all even no
{
for(var j = 3; j*j <= i; j=j+2) //check by upto sqrt(i), skip all even no
{
if(i % j == 0) {
flag = 0;break; //not a prime, break
}
flag = 1;
}
if (flag == 1) prime.push(i); //prime, add to answer
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}
Because you blank your list of primes EVERY loop cycle, move it outside the for loop
You need to make your variable prime outside of your loop
This is the code you have re-written
var prime = [];
for(var i = 2; i < 100; i++)
{
for(var j = 0; j <= i; j++)
{
var p = i % j;
}
if(p != 0) prime.push(i);
else continue;
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}
I'm a fan of the sieve of Eratosthenes.
The following code should do what you wanted.
var prime = Array(101).fill(true);
for (var i = 2; i < 100; ++i){
if (prime[i]){
document.writeln(i, "<br>");
for (var j = i*i; j < 100; j += i){
prime[j] = false;
}
}
}
Or since it's only up to 100 you could just manually type the list (but, hey where's the learning if you do it that way?).
(1) Move prime outside the for loop, (2) start j at 2 and end when j < i, (3) check when p == 0 with a boolean flag and break inner loop.
var prime = []; //put prime out here so it does not reassign
for(var i = 2; i < 100; i++)
{
var isPrime = true;
for(var j = 2; j < i; j++) //start j at 2
{
var p = i % j;
if(p == 0)
{
isPrime = false;
break;
}
}
if(isPrime) prime.push(i);
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}

javascript matrix manipulation is not working

This is a bad coded solution to a problem in "advent of code": link to problem
I don't know the reason because my code is not working properly, I had an error related to regular expressions cause I didn't reset the pointer of the regexp object, now that error is fixed I think, but something is escaping to my knowledge in what I've done bad.
The problem is that the solution that my code displays is not correct, you can submit a solution on the link I've provided and get feedback of your solutions.
Correct solution: 543903
Given solution: 418954
// day 6 of advent of code
var input = "removed, take from problem";
function processInput(input, matrix) {
var linesOfInput = input.split("\n");
var matches;
var turnOnRE = /turn on (\d+),(\d+).*?(\d+),(\d+)/g;
var turnOffRE = /turn off (\d+),(\d+).*?(\d+),(\d+)/g;
var toggleRE = /toggle (\d+),(\d+).*?(\d+),(\d+)/g;
// regular expression objects lastIndex property must be 'reseted' in order to work well
for (var i = 0 ; i < linesOfInput.length; i++) {
turnOnRE.lastIndex = 0;
turnOffRE.lastIndex = 0;
toggleRE.lastIndex = 0;
matches = turnOnRE.exec(linesOfInput[i]);
if (matches != null) {
manipulateLights(matrix, matches[1], matches[2], matches[3], matches[4], true);
continue;
}
matches = turnOffRE.exec(linesOfInput[i]);
if (matches != null) {
manipulateLights(matrix, matches[1], matches[2], matches[3], matches[4], false);
continue;
}
matches = toggleRE.exec(linesOfInput[i]);
manipulateLights(matrix, matches[1], matches[2], matches[3], matches[4]);
}
}
function manipulateLights(matrix, startI, startJ, endI, endJ, newValue) {
if (newValue == undefined) { // toogle
for (var i = startI ; i <= endI; i++) {
for (var j = startJ ; j <= endJ; j++) {
matrix[i][j] = !matrix[i][j];
}
}
console.log(startI, startJ, endI, endJ, newValue);
} else {
for (var i = startI ; i <= endI; i++) {
for (var j = startJ ; j <= endJ; j++) {
matrix[i][j] = newValue;
}
}
console.log(startI, startJ, endI, endJ, newValue);
}
console.log(countTurnedOnLights(matrix));
}
function countTurnedOnLights(matrix) {
var turnedOn = 0;
for (var i = 0 ; i < matrixWidth; i++) {
for (var j = 0 ; j < matrixHeigth; j++) {
if (matrix[i][j] == true) {
turnedOn++;
}
}
}
return turnedOn;
}
var matrixHeigth = 1000;
var matrixWidth = 1000;
// define a bidimensional array, is almost like in C++
var lightMatrix = new Array(matrixWidth);
for (var i = 0 ; i < matrixWidth; i++) {
lightMatrix[i] = new Array(matrixHeigth);
}
// turn off all lights
for (var i = 0 ; i < matrixWidth; i++) {
for (var j = 0 ; j < matrixHeigth; j++) {
lightMatrix[i][j] = false;
}
}
processInput(input, lightMatrix);
console.log(countTurnedOnLights(lightMatrix));
OK I figured out the error - your regular expression matches are being treated as strings when you first create your for loops.
for (var i = startI ; i <= endI; i++) {
for (var j = startJ ; j <= endJ; j++) {
When you hit a combo like 756,53 through 923,339, it thinks 53 > 339 and it exits the loop immediately. Either wrap each "start" variable with Number() in your for loops, or do so when passing the parameters.

How to generate 100 random numbers and checking each one for primality?

Write a Boolean function named isPrime which takes an integer as an argument and returns true if the argument is a prime number of false otherwise. Generate 100 random numbers and display the results of checking each one for primality.
This is supposed to output the random numbers that are prime (after the true or false check) but I am getting the results of 2 sets of numbers in order.
Here is my code:
var arr = []
while(arr.length < 100){
var randomnumber=Math.ceil(Math.random()*100)
var found=false;
for(var i=0;i<arr.length;i++){
if(arr[i]==randomnumber){found=true;break}
}
if(!found)arr[arr.length]=randomnumber;
}
console.log(arr);
for(i = 0; i < 100; i++){
if(isPrime(i)) console.log(i);
}
function isPrime(num) {
if(num < 2) return false;
for (var i = 2; i < num; i++) {
if(num%i==0)
return false;
}
return true;
}
You need to check for primality of arr[i] instead of i:
for(i = 0; i < 100; i++){
if(isPrime(arr[i])) console.log(arr[i]);
}
function isPrime(value) {
for(var i = 2; i < value; i++) {
if(value % i === 0) {
return false;
}
}
return value > 1;
}
function myFunction() {
for(var i = 0; i < 100; i++) {
$('#demo').append('<div>'+i+'*****'+isPrime(i)+'</div>');
}
}
try it in an html page and dont forget to add jquery
The below code part, you are trying to check the numbers from 0 to 100
for(i = 0; i < 100; i++){
if(isPrime(i)) console.log(i);
}
But you should check the arr array one by one
for(i = 0; i < 100; i++){
if(isPrime(arr[i])) console.log(arr[i]);
}
Using an object to store the numbers let's you avoid the loop to check for duplicates and moving the check for isPrime () let's you skip the second loop. (Your issue was, as #Andrea pointed out, not passing arr [i] to isPrime)
var myRandObj = {};
var randomNumber = 0;
for (var i = 0; i < 100; i++) {
do {
randomNumber = Math.ceil(Math.random() * 100);
} while (typeof myRandObj[randomNumber] !== 'undefined');
myRandObj[randomNumber] = 0;
if (isPrime (randomNumber))
console.log (randomNumber);
}
console.log(arr) is printing the entire array to the console. Remove that line to debug further.
var arr = []
for(x = 0; x < 100; x++){
arr[x] = Math.ceil(Math.random()*100)
}
for(i = 0; i < 100; i++){
if(isPrime(arr[i])) console.log(arr[i]);
}
function isPrime(num) {
if(num < 2) return false;
for (var i = 2; i < num; i++) {
if(num%i==0)
return false;
}
return true;
}

prime number nth length

I need a way to find the 10001th prime number. My code prints out all the prime numbers but I'm not sure how to get the 10001th prime number.
function prime(nth) {
var arr = [2];
var isPrime = true;
for(var i = 3; i <= 100; i++) {
isPrime = true;
for(var j = 2; j < i; j++) {
if(i % j === 0) {
isPrime = false;
break;
}
}
if(isPrime === true) {
arr.push(i);
}
}
return arr;
}
prime();
Use the length of the prime array as the looping condition of your for loop, not the number you're testing.
function prime(nth) {
var arr = [2];
var isPrime = true;
for(var i = 3; arr.length <= nth; i++) {
isPrime = true;
for(var j = 2; j < i; j++) {
if(i % j === 0) {
isPrime = false;
break;
}
}
if(isPrime === true) {
arr.push(i);
}
}
return arr;
}

Categories

Resources