Skip to content

Commit 71bbd07

Browse files
authored
Merge pull request #88 from PatOnTheBack/master
Improved JSLint Compliance and Created Math Algorithms
2 parents f4e6b66 + 6660037 commit 71bbd07

12 files changed

+289
-143
lines changed

Algorithms/EucledianGCD.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
function euclideanGCDRecursive (first, second) {
2-
/*
3-
Calculates GCD of two numbers using Euclidean Recursive Algorithm
4-
:param first: First number
5-
:param second: Second number
6-
:return: GCD of the numbers
7-
*/
8-
if (second == 0) {
9-
return first;
10-
} else {
11-
return euclideanGCDRecursive(second, (first % second));
12-
}
1+
function euclideanGCDRecursive(first, second) {
2+
/*
3+
Calculates GCD of two numbers using Euclidean Recursive Algorithm
4+
:param first: First number
5+
:param second: Second number
6+
:return: GCD of the numbers
7+
*/
8+
if (second === 0) {
9+
return first;
10+
} else {
11+
return euclideanGCDRecursive(second, (first % second));
12+
}
1313
}
1414

15-
function euclideanGCDIterative (first, second) {
16-
/*
17-
Calculates GCD of two numbers using Euclidean Iterative Algorithm
18-
:param first: First number
19-
:param second: Second number
20-
:return: GCD of the numbers
21-
*/
22-
while (second != 0) {
23-
let temp = second;
24-
second = first % second;
25-
first = temp;
26-
}
27-
return first;
15+
function euclideanGCDIterative(first, second) {
16+
/*
17+
Calculates GCD of two numbers using Euclidean Iterative Algorithm
18+
:param first: First number
19+
:param second: Second number
20+
:return: GCD of the numbers
21+
*/
22+
while (second !== 0) {
23+
let temp = second;
24+
second = first % second;
25+
first = temp;
26+
}
27+
return first;
2828
}
2929

30-
function main () {
31-
let first = 20;
32-
let second = 30;
33-
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
34-
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
30+
function main() {
31+
let first = 20;
32+
let second = 30;
33+
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
34+
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
3535
}
3636

3737
main();

Algorithms/sieveOfEratosthenes.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
function sieveOfEratosthenes (n) {
2-
/*
3-
* Calculates prime numbers till a number n
4-
* :param n: Number upto which to calculate primes
5-
* :return: A boolean list contaning only primes
6-
*/
7-
let primes = new Array(n + 1);
8-
primes.fill(true); // set all as true initially
9-
primes[0] = primes[1] = false; // Handling case for 0 and 1
10-
let sqrtn = Math.ceil(Math.sqrt(n));
11-
for (let i = 2; i <= sqrtn; i++) {
12-
if (primes[i]) {
13-
for (let j = 2 * i; j <= n; j += i) {
14-
primes[j] = false;
15-
}
16-
}
17-
}
18-
return primes;
1+
function sieveOfEratosthenes(n) {
2+
/*
3+
* Calculates prime numbers till a number n
4+
* :param n: Number upto which to calculate primes
5+
* :return: A boolean list contaning only primes
6+
*/
7+
let primes = new Array(n + 1);
8+
primes.fill(true); // set all as true initially
9+
primes[0] = primes[1] = false; // Handling case for 0 and 1
10+
let sqrtn = Math.ceil(Math.sqrt(n));
11+
for (let i = 2; i <= sqrtn; i++) {
12+
if (primes[i]) {
13+
for (let j = 2 * i; j <= n; j += i) {
14+
primes[j] = false;
15+
}
16+
}
17+
}
18+
return primes;
1919
}
2020

21-
function main () {
22-
let n = 69; // number till where we wish to find primes
23-
let primes = sieveOfEratosthenes(n);
24-
for (let i = 2; i <= n; i++) {
25-
if (primes[i]) {
26-
console.log(i);
27-
}
28-
}
21+
function main() {
22+
let n = 69; // number till where we wish to find primes
23+
let primes = sieveOfEratosthenes(n);
24+
for (let i = 2; i <= n; i++) {
25+
if (primes[i]) {
26+
console.log(i);
27+
}
28+
}
2929
}
3030

3131
main();

Ciphers/caesarsCipher.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Caesar's Cipher - also known as the ROT13 Cipher is when
3-
* a letter is replaced by the one that is 13 spaces away
3+
* a letter is replaced by the one that is 13 spaces away
44
* from it in the alphabet. If the letter is in the first half
55
* of the alphabet we add 13, if it's in the latter half we
66
* subtract 13 from the character code value.
@@ -12,27 +12,27 @@
1212
* @return {String} decrypted string
1313
*/
1414
function rot13(str) {
15-
let response = [];
16-
let strLength = str.length;
15+
let response = [];
16+
let strLength = str.length;
1717

18-
for (let i =0; i < strLength; i++) {
19-
const char = str.charCodeAt(i);
18+
for (let i = 0; i < strLength; i++) {
19+
const char = str.charCodeAt(i);
20+
21+
if (char < 65 || (char > 90 && char < 97) || char > 122) {
22+
response.push(str.charAt(i));
23+
} else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) {
24+
response.push(String.fromCharCode(str.charCodeAt(i) - 13));
25+
} else {
26+
response.push(String.fromCharCode(str.charCodeAt(i) + 13));
27+
}
2028

21-
if (char < 65 || (char > 90 && char < 97) || char > 122) {
22-
response.push(str.charAt(i));
23-
} else if ((char > 77 && char <= 90 ) || (char > 109 && char <= 122)) {
24-
response.push(String.fromCharCode(str.charCodeAt(i) - 13));
25-
} else {
26-
response.push(String.fromCharCode(str.charCodeAt(i) + 13));
2729
}
28-
29-
}
30-
return response.join('');
30+
return response.join("");
3131
}
3232

3333

3434
// Caesars Cipher Example
35-
const encryptedString = 'Uryyb Jbeyq';
35+
const encryptedString = "Uryyb Jbeyq";
3636
const decryptedString = rot13(encryptedString);
3737

38-
console.log(decryptedString); // Hello World
38+
console.log(decryptedString); // Hello World

Conversions/DecimalToBinary.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function decimalToBinary(num) {
44
bin.unshift(num % 2);
55
num >>= 1; // basically /= 2 without remainder if any
66
}
7-
console.log("The decimal in binary is " + bin.join(''));
7+
console.log("The decimal in binary is " + bin.join(""));
88
}
99

1010
decimalToBinary(2);

Search/binarySearch.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/*Binary Search-Search a sorted array by repeatedly dividing the search interval
2-
* in half. Begin with an interval covering the whole array. If the value of the
3-
* search key is less than the item in the middle of the interval, narrow the interval
4-
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
5-
* value is found or the interval is empty.
6-
*/
2+
* in half. Begin with an interval covering the whole array. If the value of the
3+
* search key is less than the item in the middle of the interval, narrow the interval
4+
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
5+
* value is found or the interval is empty.
6+
*/
77

88
function binarySearch(arr, i) {
99
var mid = Math.floor(arr.length / 2);
1010
if (arr[mid] === i) {
11-
console.log('match', arr[mid], i);
11+
console.log("match", arr[mid], i);
1212
return arr[mid];
1313
} else if (arr[mid] < i && arr.length > 1) {
1414
binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
1515
} else if (arr[mid] > i && arr.length > 1) {
1616
binarySearch(arr.splice(0, mid), i);
1717
} else {
18-
console.log('not found', i);
18+
console.log("not found", i);
1919
return -1;
2020
}
2121

2222
}
2323

24-
var ar=[1,2,3,4,5,6,7,8,9,10];
25-
binarySearch(ar,3);
26-
binarySearch(ar,7);
27-
binarySearch(ar,13);
24+
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
25+
binarySearch(ar, 3);
26+
binarySearch(ar, 7);
27+
binarySearch(ar, 13);

Search/linearSearch.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/*
2-
* Linear search or sequential search is a method for finding a target
3-
* value within a list. It sequentially checks each element of the list
4-
* for the target value until a match is found or until all the elements
5-
* have been searched.
6-
*/
2+
* Linear search or sequential search is a method for finding a target
3+
* value within a list. It sequentially checks each element of the list
4+
* for the target value until a match is found or until all the elements
5+
* have been searched.
6+
*/
77
function SearchArray(searchNum, ar) {
8-
var position = Search(ar, searchNum);
9-
if (position != -1) {
10-
console.log("The element was found at " + (position + 1));
11-
} else {
12-
console.log("The element not found");
13-
}
8+
var position = Search(ar, searchNum);
9+
if (position != -1) {
10+
console.log("The element was found at " + (position + 1));
11+
} else {
12+
console.log("The element not found");
13+
}
1414
}
1515

1616
// Search “theArray” for the specified “key” value
1717
function Search(theArray, key) {
18-
for (var n = 0; n < theArray.length; n++)
19-
if (theArray[n] == key)
20-
return n;
21-
return -1;
18+
for (var n = 0; n < theArray.length; n++)
19+
if (theArray[n] == key)
20+
return n;
21+
return -1;
2222
}
2323

2424
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9];

Sorts/bogoSort.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
/*
2-
* A simple helper function that checks, if the array is
3-
* sorted in ascending order.
4-
*/
5-
Array.prototype.isSorted = function() {
2+
* A simple helper function that checks, if the array is
3+
* sorted in ascending order.
4+
*/
5+
Array.prototype.isSorted = function () {
66

7-
let length = this.length;
7+
let length = this.length;
88

9-
if (length < 2) {
10-
return true;
11-
}
9+
if (length < 2) {
10+
return true;
11+
}
1212

13-
for (let i = 0; i < length - 1; i++) {
14-
if (this[i] > this[i + 1]) {
15-
return false;
13+
for (let i = 0; i < length - 1; i++) {
14+
if (this[i] > this[i + 1]) {
15+
return false;
16+
}
1617
}
17-
}
18-
return true;
18+
return true;
1919
};
2020

2121
/*
22-
* A simple helper function to shuffle the array randomly in place.
23-
*/
24-
Array.prototype.shuffle = function() {
25-
26-
for (let i = this.length -1; i; i--) {
27-
let m = Math.floor(Math.random() * i);
28-
let n = this[i - 1];
29-
this[i - 1] = this[m];
30-
this[m] = n;
31-
}
22+
* A simple helper function to shuffle the array randomly in place.
23+
*/
24+
Array.prototype.shuffle = function () {
25+
26+
for (let i = this.length - 1; i; i--) {
27+
let m = Math.floor(Math.random() * i);
28+
let n = this[i - 1];
29+
this[i - 1] = this[m];
30+
this[m] = n;
31+
}
3232

3333
};
3434

3535
/*
36-
* Implementation of the bogosort algorithm. This sorting algorithm randomly
37-
* rearranges the array until it is sorted.
38-
* For more information see: https://en.wikipedia.org/wiki/Bogosort
39-
*/
36+
* Implementation of the bogosort algorithm. This sorting algorithm randomly
37+
* rearranges the array until it is sorted.
38+
* For more information see: https://en.wikipedia.org/wiki/Bogosort
39+
*/
4040
function bogoSort(items) {
4141

42-
while(!items.isSorted()){
43-
items.shuffle()
44-
}
45-
return items;
42+
while (!items.isSorted()) {
43+
items.shuffle()
44+
}
45+
return items;
4646
}
4747

4848
//Implementation of bogoSort
@@ -52,4 +52,4 @@ var ar = [5, 6, 7, 8, 1, 2, 12, 14];
5252
console.log(ar);
5353
bogoSort(ar);
5454
//Array after sort
55-
console.log(ar);
55+
console.log(ar);

Sorts/wiggleSort.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
2-
* Wiggle sort sorts the array into a wave like array.
3-
* An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
4-
*
5-
*/
2+
* Wiggle sort sorts the array into a wave like array.
3+
* An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
4+
*
5+
*/
66

7-
Array.prototype.wiggleSort = function() {
8-
for (let i = 0; i < this.length; ++i) {
9-
const shouldNotBeLessThan = i % 2;
10-
const isLessThan = this[i] < this[i + 1];
11-
if (shouldNotBeLessThan && isLessThan) {
12-
[this[i], this[i + 1]] = [this[i + 1], this[i]];
7+
Array.prototype.wiggleSort = function () {
8+
for (let i = 0; i < this.length; ++i) {
9+
const shouldNotBeLessThan = i % 2;
10+
const isLessThan = this[i] < this[i + 1];
11+
if (shouldNotBeLessThan && isLessThan) {
12+
[this[i], this[i + 1]] = [this[i + 1], this[i]];
13+
}
1314
}
14-
}
15-
return this;
15+
return this;
1616
};
1717

1818
//Implementation of wiggle sort

0 commit comments

Comments
 (0)