print prime nos upto 100 in javascript - javascript

<!DOCTYPE HTML>
<html>
<head>
<script>
var i, j, k = 1;
for (i = 3; i < 100; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
k = 0;
break
}
}
}
if (k == 1) {
alert(i);
}
</script>
</head>
<body>
</body>
</html>
I am trying to print prime nos in js from 2 to 100.However the code is not producing the desired result.Please help.Thanks in advance.

First you only need to check till j <= (i/2) in your for loop.
also your if(k==1) need to be inside the outer for loop
var i, j;
for (i = 3; i < 100; i++) {
var k = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
prime = false;
k = 0;
break;
}
}
if (k == 1) {
console.log(i);
}
}

Related

longest common substring Multiple Input in JS

function LCSubStr(X, Y) {
let m = X.length;
let n = Y.length;
let result = 0;
let end;
let len = new Array(4);
for (let i = 0; i < len.length; i++) {
len[i] = new Array(n);
for (let j = 0; j < n; j++) {
len[i][j] = 0;
}
}
let currRow = 0;
for (let i = 0; i <= m; i++) {
for (let j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
len[currRow][j] = 0;
}
else if (X[i - 1] == Y[j - 1]) {
len[currRow][j] = len[1 - currRow][j - 1] + 1;
if (len[currRow][j] > result) {
result = len[currRow][j];
end = i - 1;
}
}
else {
len[currRow][j] = 0;
}
}
currRow = 1 - currRow;
}
if (result == 0) {
return "-1";
}
return X.substr(end - result + 1, result);
}
// Driver Code
let X = "GeeksforGeeks";
let Y = "GeeksQuiz";
// function call
document.write(LCSubStr(X, Y));
How can I convert this code for multiple input?
I checked many lcs code but no one works with
ABCQEFDEFGHIJ BCXEFGYZBCDEWEFGHU > EFGH
This one just works good without any problem. I should convert this one for multiple input in Javascript.
Now we have X,Y but it shoulde be with multiple inputs.

Why this code printing wrong prime numbers?

"I think problem is in for loop. Because i have used same "j" to iteration in both function's loop."
I wanted to print all prime number from 1-100.
I think all code is good. But it kept showing output other than prime numbers.
I can't find answers myself . And no other places has written answer. Please help me out here. I want to understand this problem.
var n = 100;
var prime1 = new Array();
//producing prime number upto 97
function primeNumber() {
for (j = 1; j <= n; j++) {
if (countRemainder(j) == 2) {
prime1.push(j);
}
}
}
primeNumber();
console.log(prime1);
function countRemainder(n) {
var count = 0;
for (j = 1; j <= n; j++) {
if (n % j == 0) {
count++;
}
}
return count;
}
The problem was indeed happening because you were using the same variable in both for loops.
When you declare a variable like j = 0; and not like var j = 0; the variable will be added to global scope (instead of the scope you are in, so every other scope can see and alter that variable).
If your script is running in strict mode, then this will throw an error, instead of adding the variable to the global scope.
So just add var before each j declaration.
var n = 100;
var prime1 = new Array();
//producing prime number upto 97
function primeNumber() {
for (var j = 1; j <= n; j++) {
if (countRemainder(j) == 2) {
prime1.push(j);
}
}
}
primeNumber();
console.log(prime1);
function countRemainder(n) {
var count = 0;
for (var j = 1; j <= n; j++) {
if (n % j == 0) {
count++;
}
}
return count;
}
Use "let" to declare "j" correctly.
function primeNumber() {
for (let j = 1; j <= n; j++) {
if (countRemainder(j) == 2) {
prime1.push(j);
}
}
}
Try this
var n = 100;
var prime1 = new Array();
//producing prime number upto 97
function primeNumber() {
for (var j = 1; j <= n; j++) {
if (countRemainder(j) == 2) {
prime1.push(j);
}
}
}
primeNumber();
console.log(prime1);
function countRemainder(n) {
var count = 0;
for (var j = 1; j <= n; j++) {
if (n % j == 0) {
count++;
}
}
return count;
}

Javascript Array manipulation and looping

Output of the following code is given after the program. I need to iterate j value in the following order (1,2,3,4),(2,3,4,1),(3,4,1,2),(4,1,2,3) but it is in order of(1,2,3,4),(2,3,4),(3,4),(4) . Any help is much appreciated
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
for (i=0; i < n; i++){
for (j=i+1; j<n ;j++){
document.write("</br>");
document.write("i is "+i+" j is "+j);
var h = (RRIntervalArrayy[j] - RRIntervalArrayy[i]);
document.write("</br>");
if(h < 0.12){
validRRIntervalCount++;
}
document.write(h);
if(j==(n-1)){
document.write("</br>");
document.write(validRRIntervalCount)
break;
}
}
validRRIntervalCount = 0;
document.write("</br>");
}
output
i is 0 j is 1
0.030000000000000027
i is 0 j is 2
-0.21999999999999997
i is 0 j is 3
1.92
i is 0 j is 4
0.030000000000000027
3
i is 1 j is 2
-0.25
i is 1 j is 3
1.8900000000000001
i is 1 j is 4
0
2
i is 2 j is 3
2.14
i is 2 j is 4
0.25
0
i is 3 j is 4
-1.8900000000000001
1
You need to set j as 0 when it comes to the end of the array. And the double loop is useless.
var i=0;
var j=i;
for(i=0; i<arrayLength ; i++){
j++;
if(j==arrayLength){
j=0;
}
//Do your stuff
}
Instead of managing complex looping, you can just shift your element from start to end.
RRIntervalArrayy.push(RRIntervalArrayy.shift());
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
for (i=0; i < n; i++){
document.write("--------------------------------");
document.write("</br>");
for (j=0; j<n ;j++){
document.write("</br>");
document.write("array element is:" + RRIntervalArrayy[j] );
var h = (RRIntervalArrayy[j] - RRIntervalArrayy[i]);
document.write("</br>");
if(h < 0.12){
validRRIntervalCount++;
}
document.write(h);
if(j==(n-1)){
document.write("</br>");
document.write(validRRIntervalCount)
break;
}
}
RRIntervalArrayy.push(RRIntervalArrayy.shift());
validRRIntervalCount = 0;
document.write("</br>");
}
Just to print in the order you need, try this:
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
var revert = 0;
for (i=0; i < n; i++){
revert = 0;
for (j=i+1; j< n ;j++){
if(revert == 1 && j == i){
break;
}else if(revert == 1 && j != i){
document.write(j + 1);
continue;
}
document.write(j);
if(j==(n-1)){
if(revert == 0){
j = -1;
revert = 1;
}
}
}
validRRIntervalCount = 0;
document.write("</br>");
}
To strictly do what you want:
for (i=0; i < n; i++){
var j = i + 1
var count = 0
while (count < n-1){
if (j == n)
j = 1
// do your stuff
count++
j++
}
// do your stuff
}
Just a little bit of modulus magic can do he Job for you
var RRIntervalArrayDiff = [];
var validRRIntervalCount = 0;
var RRIntervalArrayy = [0.62, 0.65, 0.40, 2.54, 0.65];
var n = RRIntervalArrayy.length;
for (i = 0; i < n; i++) {
for (j = i; j < n + i; j++) {
var newJ = j % (n) + 1;
//use newJ instead of j in your calculations
}
}

How to print a star pattern?

I want to print the following star pattern using JavaScript:
*****
***
*
***
*****
And I am trying this code, where I am getting off a triangle:
for (i = 3; i >= 1; i--) {
/* Printing spaces */
for (j = 0; j <= 3 - i; j++) {
document.write("&nbsp");
}
/* Printing stars */
k = 0;
while (k != (2 * i - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
}
try this code
after i==1 create new pattern inside it
for (i = 3; i >= 1; i--) {
/* Printing spaces */
for (j = 0; j <= 3 - i; j++) {
document.write("&nbsp");
}
/* Printing stars */
k = 0;
while (k != (2 * i - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
if (i == 1) {
for (t = 2; t <= 3; t++) {
for (j = 0; j <= 3 - t; j++) {
document.write("&nbsp");
}
/* Printing stars */
k = 0;
while (k != (2 * t - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
}
}
}
Try this code:-
In this we use outer loop and inner loop.
var i, j;
//outer loop
for(i=1; i <= 5; i++)
{
//inner loop
for(j=1; j<=i; j++)
{
document.write('*');
}
document.write('<br/>');
}
Iteration:-
i j(Repeat)
1 1
2 2
3 3
4 4
5 5
const n = 5
for (var i = 0; i < n; i++) {
var star = '';
for (var j = 0; j < n - i; j++) {
star += ' ';
}
for (var k = 0; k < i; k++) {
star += '* ';
}
console.log(star);
}
for (var i = 0; i < n; i++) {
var star = '';
for (var b = 0; b < i; b++) {
star += ' ';
}
for (var a = 0; a < n - i; a++) {
star += '* ';
}
console.log(star);
}

javascript - missing ) after for-loop control

Im trying to solve problem #4 on project Euler,im using a simple for-loop to sift through each element of the array and "missing ) after for-loop control"
Code below
var palidrome = function (num) {
var numstr = (num).toString().split("");
var count = 0;
for (var i = 0, i2 = numstr.length - 1; i < numstr.length / 2 && i2 >= numstr.length / 2; i++, i2--) {
if (numstr[i] !== numstr[i2]) {
return 0;
} else {
if (count == 3) {
return numstr.join("");
}
}
count++;
}
};
for (var i = 999; i >= 100; i--) {
for (var j = 100; j = < i; j++) {
if (palidrome(i * j) !== 0) {
alert(palidrome(i * j));
break;
}
}
}
Thank you for the assistance,much appreciated.
In for loop you have error: j = < i must be j <= i
for (var i = 999; i >= 100; i--) {
for (var j = 100; j <= i; j++) {
if (palidrome(i * j) !== 0) {
alert(palidrome(i * j));
break;
}
}
}

Categories