JavaScript prototype to find co-primes

Co-primes are numbers whose common factor is only 1. For example :- 4, 9
Factors of 4 :- 2, 2, 1
Factors of 9 :- 3, 3, 1
Common Factor :- 1

Here is the JavaScript prototype to find co-primes. Here is the code snippet.


Code (
Run Code):

function factors(num) {
arr = [];
for (var i = 1; i < num; i++) {
if (num % i == 0) {
arr.push(i);
}
}
return arr;
}

function coPrime(num1, num2) {
arr1 = factors(num1);
arr2 = factors(num2);
for (var i = 1; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
return num1 + ' and ' + num2 + ' are not co-prime';
}
}
}
return num1 + ' and ' + num2 + ' are co-prime';
}

function main() {
document.getElementById('result').innerHTML =
(coPrime(document.getElementById('num1').value, document.getElementById('num2').value))
}

JavaScript prototype to find opposite primes

Opposite Primes are prime numbers that have the same digits but in reverse order.

Here is the JavaScript prototype to find opposite primes up to a maximum limit of 100. Here is the code snippet.


Code (
Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeNumbers(num) {
var arr = [];
for (var j = 2; j < num; j++) {
if (checkPrime(j)) {
arr.push(j);
}
}
return arr;
}

function oppositePrimes() {
var num = Number(document.getElementById('num').value);
var arr = primeNumbers(num);
var text = '';
text += 'Prime numbers till ' + num + ' are: ';
text += '<br>' + arr;
text += '<br> Opposite Primes:'
for (var i = 0; i < arr.length; i++) {
for (var x = i + 1; x < arr.length; x++) {
if (arr[i].toString()[0] ===
arr[x].toString()[1] && arr[i].toString()[1] === arr[x].toString()[0]) {
text += '<br>' + arr[i] + ' and ' + arr[x];
}
}
}
document.getElementById('result').innerHTML = text;
}

JavaScript prototype to find twin primes

When 2 prime numbers are subtracted and their result is 2 then they are known as twin primes.

Here is the JavaScript prototype to find twin primes up to a given limit. Here is the code snippet.


Code (Run Code):

arr = [];

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeNumbers() {
num = Number(document.getElementById('number').value);
for (var j = 1; j < num; j++) {
if (checkPrime(j)) {
arr.push(j);
}
}
}

function twinPrimes() {
var text = '';
arr = [];
primeNumbers();
text += 'Prime Numbers:';
text += '<br>' + arr;
text += '<br> Twin Primes:'
for (var i = 0; i < arr.length; i++) {
for (var x = i + 1; x < arr.length; x++) {
if ((arr[i] - arr[x]) == -2) {
text += '<br>' + arr[x] + ' and ' + arr[i];
}
}
}
document.getElementById('result').innerHTML = text;
}

JavaScript 'For Loops'

A little glimpse into JavaScript 'For Loops'. 


Code:

arr = ['Fortran', 'Python', 'C++', 'Assembly', 'Binary'];

for (x in arr) {
document.write(x);
}
document.write('<br>');
for (var i = 0; i < arr.length; i++) {
document.write(i);
}
document.write('<br>');
for (x of arr) {
document.write(x + ' ');
}
document.write('<br>');
for (var i = 0; i < arr.length; i++) {
document.write(arr[i] + ' ');
}

Result:

01234
01234
Fortran Python C++ Assembly Binary
Fortran Python C++ Assembly Binary

JavaScript prototype to find two primes whose sum is the given number

This program finds two prime numbers whose sum is the number provided by the user.

Took 8 minutes and 3 seconds to code JavaScript prototype to find two primes whose sum is the given number. Here is the code snippet.


Code (Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeSum(num) {
arr = [];
for (var i = 2; i < num; i++) {
if (checkPrime(i)) {
arr.push(i);
}
}
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] == num) {
return arr[i] + ' + ' + arr[j] + ' = ' + num;
}
}
}
}

function main(){
document.getElementById("result").innerHTML =
primeSum(document.getElementById("num").value);
}

JavaScript prototype to calculate LCM of two numbers

LCM is Lowest Common Multiple. For example :- LCM of 4 and 6 is 12.
Prime Factors of 4 = 2, 2
Prime Factors of 6 = 2, 3
So the LCM of 4 and 6 is 2 x 2 x 3 = 12.

Took 15 minutes and 21 seconds to code JavaScript prototype to calculate LCM of two numbers. Here is the code snippet. 


Code (Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeFactor(num) {
for (var i = 2; i < num; i++) {
if (num % i == 0) {
return i;
}
}
}

function primeFactors(num) {
arr = [];
x = num;
while (!checkPrime(x)) {
arr.push(primeFactor(x));
x = x / primeFactor(x);
}
arr.push(x);
return arr;
}

function calculateLCM(num1, num2) {
arr1 = primeFactors(num1);
arr2 = primeFactors(num2);
arr3 = [];
for (var i = 0; i < arr1.length; i++) {
arr3.push(arr1[i])
for (var j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
delete arr2[j];
break;
}
}
}
for (var i = 0; i < arr2.length; i++) {
if (arr2[i] != undefined) {
arr3.push(arr2[i]);
}
}
product = arr3[0];
for (var y = 1; y < arr3.length; y++) {
product *= arr3[y];
}
return product;
}

function main(){
document.getElementById("result").innerHTML =
calculateLCM(document.getElementById("num1").value, document.getElementById("num2").value);
}

JavaScript prototype to calculate HCF of two numbers

HCF is Highest Common Factor. For example :- HCF of 4 and 6 is 2.
Prime Factors of 4 = 2, 2 
Prime Factors of 6 = 2, 3 
So 2 is common in both, hence the HCF of 4 and 6 is 2.

Took  32 minutes and 25 seconds to code JavaScript prototype to calculate HCF of two numbers. Here is the code snippet.


Code (Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeFactor(num) {
for (var i = 2; i < num; i++) {
if (num % i == 0) {
return i;
}
}
}

function primeFactors(num) {
arr = [];
x = num;
while (!checkPrime(x)) {
arr.push(primeFactor(x));
x = x / primeFactor(x);
}
arr.push(x);
return arr;
}

function calculateHCF(num1, num2) {
arr1 = primeFactors(num1);
arr2 = primeFactors(num2);
arr3 = [];
for (var i = 0; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
arr3.push(arr1[i]);
delete arr1[i];
delete arr2[j];
}
}
}
product = arr3[0];
for (var y = 1; y < arr3.length; y++) {
product *= arr3[y];
}
if (product == undefined) {
return 1;
} else {
return product;
}
}

function main(){
document.getElementById("result").innerHTML =
calculateHCF(document.getElementById("num1").value, document.getElementById("num2").value);
}

JavaScript code to find prime factors

Took 4 minutes and 48 seconds to code JavaScript prototype to find prime factors of a given number. Here is the code snippet.

Code (Run Code):

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeFactor(num) {
for (var i = 2; i < num; i++) {
if (num % i == 0) {
return i;
}
}
}

function primeFactors(num) {
arr = [];
x = num;
while (!checkPrime(x)) {
arr.push(primeFactor(x));
x = x / primeFactor(x);
}
arr.push(x);
return arr;
}

function main() {
document.getElementById('demo').innerHTML =
primeFactors(document.getElementById('num').value);
}

JavaScript code to generate composite number series

Took only 59 seconds to code JavaScript prototype to generate composite number series. Here is the code snippet.

Code (Run Code):

arr = [];
function checkComposite(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return false;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return true;
}
}
return false;
}

function compositeNumbers() {
num = Number(document.getElementById('number').value);
for (var j = 1; j < num; j++) {
if (checkComposite(j)) {
arr.push(j);
}
}
document.getElementById('result').innerHTML = arr;
}

JavaScript code to check for composite numbers

Took 5 minutes and 22 seconds to code JavaScript prototype to check for composite numbers. Here is the code snippet.

Code (Run Code):

function checkComposite(num) {
if (num == 1) {
return num + ' is not a prime number and not a composite number';
} else if (num == 2) {
return num + ' is not a composite number';
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return num + ' is a composite number';
}
}
return num + ' is not a composite number';
}

function main(){
document.getElementById("result").innerHTML =
checkComposite(document.getElementById("num").value);
}

JavaScript code to generate prime number series

Took only 55 seconds to code JavaScript prototype to generate prime number series. Here is the code snippet.

Code (Run Code):

arr = [];

function checkPrime(num) {
if (num == 1) {
return false;
} else if (num == 2) {
return true;
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return false;
}
}
return true;
}

function primeNumbers() {
num = Number(document.getElementById('number').value);
for (var j = 1; j < num; j++) {
if (checkPrime(j)) {
arr.push(j);
}
}
document.getElementById('result').innerHTML = arr;
}

JavaScript code to check for prime numbers

Took 10 minutes and 16 seconds to code JavaScript prototype to check for prime numbers. Here is the code snippet.

Code (Run Code):

function checkPrime(num) {
if (num == 1) {
return num + ' is not a prime number and not a composite number';
} else if (num == 2) {
return num + ' is a prime number';
}

for (var x = 2; x < num; x++) {
if (num % x == 0) {
return num + ' is not a prime number';
}
}
return num + ' is a prime number';
}

function main(){
document.getElementById("result").innerHTML =
checkPrime(document.getElementById("num").value);
}

A simple calculator coded in JavaScript

Took 19 minutes and 28 seconds to code a simple calculator in JavaScript. Here is the code snippet.


Code (Run Code):

function calculator() {
var operand1 = Number(document.getElementById('operand1').value);
var operator = document.getElementById('operator').value;
var operand2 = Number(document.getElementById('operand2').value);
switch(operator) {
case '+':
text = operand1 + ' + ' + operand2 + ' = ' + (operand1 + operand2);
break;
case '-':
text = operand1 + ' - ' + operand2 + ' = ' + (operand1 - operand2);
break;
case 'x':
case '*':
text = operand1 + ' x ' + operand2 + ' = ' + (operand1 * operand2);
break;
case '/':
text = operand1 + ' / ' + operand2 + ' = ' + (operand1 / operand2);
break;
case '**':
text = operand1 + ' ** ' + operand2 + ' = ' + (operand1 ** operand2);
break;
case '%':
text = operand1 + ' % ' + operand2 + ' = ' + (operand1 % operand2);
break;
default:
text = 'Sorry we could not calculate your equation';
}

if (isNaN(operand1) || isNaN(operand2)) {
text = 'Sorry we could not calculate your equation';
}

document.getElementById('result').innerHTML = text;
}

JavaScript method to return random number in a range (exclusive of the min limit)

Took 2 minutes and 6 seconds to code JavaScript method to return random number in a range exclusive of the min limit. Here is the code snippet.

Code:

function randomMaxInclusive(min, max) {
x = Math.floor(((Math.random() * max - min) + min) + 1);
return x;
}

x = randomMaxInclusive(0, 10)
document.write(x);

Result:

10

JavaScript method to return random number in a range (exclusive of the max limit)

Took 3 minutes and 21 seconds to code JavaScript method to return random number in a range exclusive of the max limit. Here is the code snippet.

Code:

function randomMaxExclusive(min, max) {
x = Math.floor((Math.random() * max - min) + min);
return x;
}

x = randomMaxExclusive(0, 10)
document.write(x);

Result:

4

JavaScript Boolean() method implementation

Took 1 minute and 3 seconds to code JavaScript Boolean() method. Here is the code snippet.

Code:

function boolean(condition) {
if (condition) {
return true;
} else {
return false;
}
}

document.write(boolean(0));

Result:

false

JavaScript round() method implementation

Took 5 minutes and 12 seconds to code JavaScript round() method. Here is the code snippet.

Code:

function floor(num) {
x = num;
x = x - x % 1;
return x;
}

function ceil(num) {
x = num;
x = x - x % 1;
x = x + 1;
return x;
}

function round(num) {
x = num;
if (x % 1 >= 0.5) {
ceil(x)
} else {
floor(x);
}
return x;
}

document.write(round(4.5));

Result:

5

JavaScript pow() method implementation

Took 1 minute and 13 seconds to code JavaScript pow() method. Here is the code snippet.

Code:

function pow(num1, num2) {
num3 = num1 ** num2;
return num3;
}

document.write(pow(8, 2));

Result:

64

JavaScript abs() method implementation

Took 1 minutes and 30 seconds to code JavaScript abs() method. Here is the code snippet.

Code:

function abs(num) {
num2 = num * -1;
return num2;
}

document.write(abs(-4.7));

Result:

4.7

JavaScript ceil() method implementation

Took 1 minute and 16 seconds to code JavaScript ceil() method. Here is the code snippet.

Code:

num = 4.4;
function ceil(num) {
x = num;
x = x - x % 1;
x = x + 1;
return x;
}

document.write(ceil(num));

Result:

5

JavaScript floor() method implementation

Took 1 minute and 23 seconds to code JavaScript floor() method. Here is the code snippet.

Code:

num = 4.8;
function floor(num) {
x = num;
x = x - x % 1;
return x;
}

document.write(floor(num));

Result:

4

JavaScript method to merge and sort two arrays

Took 1 minutes 56 seconds to code JavaScript method to first merge two arrays and then return the sorted array. Here is the code snippet.

Code:

arr1 = [1, 5, 6, 2];
arr2 = [2, 8, 6, 5];
function merge(arr1, arr2) {
for (var i = 0; i < arr2.length; i++) {
arr1.push(arr2[i]);
}
}

merge(arr1, arr2); //changes arr1 unlike JavaScript concat() method

function sort(arr) {
for (var i = 0; i < arr.length; i++) {
for (var y = i + 1; y < arr.length; y++) {
if (arr[i] > arr[y]) {
k = arr[i];
arr[i] = arr[y];
arr[y] = k;
}
}
}
return arr;
}

document.write(sort(arr1));

Result:

1,2,2,5,5,6,6,8

JavaScript method to sort an array

Took 5 minutes and 2 seconds to code JavaScript method to return a sorted array. Here is the code snippet.

Code:

arr = [3, 5, 2, 4];
function sort(arr) {
for (var i = 0; i < arr.length; i++) {
for (var y = i + 1; y < arr.length; y++) {
if (arr[i] > arr[y]) {
k = arr[i];
arr[i] = arr[y];
arr[y] = k;
}
}
}
return arr;
}

document.write(sort(arr));

Result:

2,3,4,5

JavaScript method to return an array with all unique elements

Took 11 minutes and 20 seconds to code JavaScript method to return an array with all unique elements. Here is the code snippet.

Code:

arr = [1,2,3,4,4,3,2,1,"1","2","3","4"];
function uniqueArray(arr) {
arr2 = [];
for (var i = 0; i < arr.length; i++) {
for (var y = i + 1; y < arr.length; y++) {
if (arr[i] == arr[y]) {
delete arr[y];
}
}
}
for (var z = 0; z < arr.length; z++) {
if (arr[z] != undefined) {
arr2.push(arr[z]);
}
}
return arr2;
}

document.write(uniqueArray(arr));

Result:

1,2,3,4

Function to add and subtract alternate array elements in JavaScript

Took 5 minutes and 10 seconds to code JavaScript method to add and subtract alternate elements in an array. Here is the code snippet.

Code:

arr = [1,2,3,4,5,6,7];
function alternateAddSubtract(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
sum += arr[i];
} else {
sum -= arr[i];
}
}
return sum;
}

document.write(alternateAddSubtract(arr));

Result:

4

Function to compute sum of all array elements in JavaScript

Took 2 minutes and 33 seconds to code a method to compute the sum of all elements of the array in JavaScript. Here is the code snippet.

Code:

arr = [1, 2, 3, 4];
function arrSum(arr) {
sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

document.write(arrSum(arr));

Result:

10

Function to compute square of each array element in JavaScript

Took 1 minute and 53 seconds to code method that returns square of each element of the array in JavaScript. Here is the code snippet.

Code:

arr = [1, 2, 3, 4];
function square(arr) {
var arr2 = [];
for (var i = 0; i < arr.length; i++) {
arr2.push(arr[i] * arr[i]);
}
return arr2;
}

document.write(square(arr));

Result:

1,4,9,16

Fibonacci series implementation in JavaScript

Fibonacci Series is a series in which the next number of the series is the sum of the previous 2 numbers. For example :- 0, 1, 1, 2, 3, 5, 8...
Took 3 minutes and 15 seconds to code fibonacci series using JavaScript. Here is the code snippet.


Code:
function printFibonacciSeries() {
var first_num = 1;
var second_num = 1;
document.getElementById('demo').innerHTML +=
first_num + "<br>" + second_num + "<br>";
for (var i = 0; i < 12; i++) {
third_num = first_num + second_num;
document.getElementById('demo').innerHTML += third_num + '<br>';
first_num = second_num;
second_num = third_num;
}
}

printFibonacciSeries();

Result:
1
1
2
3
5
8
13
21
34
55
89
144
233
377

Implementation of JavaScript find() function


Took 4 minutes 42 seconds to code JavaScript find() method that returns all elements of the array meeting a specific condition. Here is the code snippet.

Code:
arr = [18, 10, 65, 70];
function find(arr) {
var arr2 = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > 18) {
arr2.push(arr[i]);
}
}
return arr2;
}

var new_arr = find(arr);
document.write(new_arr);

Result:
65,70

Implementation of JavaScript indexOf() method


Took 3 minutes 37 seconds to code JavaScript indexOf() method. Here is the code snippet.

Code:
arr = ['1', '2', '3'];
function indexOf(arr, element) {
for (var i = 0; i < arr.length; i++) {
if (element === arr[i]) {
return i;
}
}
return -1;
}

document.write(indexOf(arr, '3'));

Result:
2

Implementation of JavaScript array min() and max() functions


Took 18 minutes and 26 seconds to create array max() method and 51 seconds to create array min() method in JavaScript. Here are the code snippets.

Function max():
arr = [1, 3, 7, 9, 11];

function max(arr) {
var maxval = arr[0];
var i = 0;
while (arr[i] != undefined) {
if (maxval < arr[i+1]) {
maxval = arr[i+1];
}
i++;
}
return maxval;
}

document.write(max(arr));

Result:
11

Function min():
arr = [1, 3, 7, 9, 11];

function min(arr) {
var minval = arr[0];
var i = 0;
while (arr[i] != undefined) {
if (minval > arr[i+1]) {
minval = arr[i+1];
}
i++;
}
return minval;
}

document.write(min(arr));

Result:
1

Create your own array reverse() method in JavaScript


Took 21 minutes and 22 seconds to create array reverse() method in JavaScript. Here is the code snippet.

Code:
var arr = ['a', 'b', 'c', 'd', 'e'];

function reverse(arr) {
var arr2 = [];
var i = arr.length-1;
while (i >= 0) {
arr2.push(arr[i]);
i--;
}
return arr2;
}

document.write(reverse(arr));

Result:
e,d,c,b,a

Coding for Kids

What is coding? In coding we build a program to do a specific task for us. Code: A code is a set of computer instructions and when you will ...