mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 19:17:33 +08:00
Fixed Whitespace, Operators, and Quotes to Comply with JSLint
I modified the whitespace in the files and changed single quotes to double quotes. I also changed some `==` and `!=` operators to `===` and `!==` to comply with JSLint.
This commit is contained in:
@ -1,37 +1,37 @@
|
|||||||
function euclideanGCDRecursive (first, second) {
|
function euclideanGCDRecursive(first, second) {
|
||||||
/*
|
/*
|
||||||
Calculates GCD of two numbers using Euclidean Recursive Algorithm
|
Calculates GCD of two numbers using Euclidean Recursive Algorithm
|
||||||
:param first: First number
|
:param first: First number
|
||||||
:param second: Second number
|
:param second: Second number
|
||||||
:return: GCD of the numbers
|
:return: GCD of the numbers
|
||||||
*/
|
*/
|
||||||
if (second == 0) {
|
if (second === 0) {
|
||||||
return first;
|
return first;
|
||||||
} else {
|
} else {
|
||||||
return euclideanGCDRecursive(second, (first % second));
|
return euclideanGCDRecursive(second, (first % second));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function euclideanGCDIterative (first, second) {
|
function euclideanGCDIterative(first, second) {
|
||||||
/*
|
/*
|
||||||
Calculates GCD of two numbers using Euclidean Iterative Algorithm
|
Calculates GCD of two numbers using Euclidean Iterative Algorithm
|
||||||
:param first: First number
|
:param first: First number
|
||||||
:param second: Second number
|
:param second: Second number
|
||||||
:return: GCD of the numbers
|
:return: GCD of the numbers
|
||||||
*/
|
*/
|
||||||
while (second != 0) {
|
while (second !== 0) {
|
||||||
let temp = second;
|
let temp = second;
|
||||||
second = first % second;
|
second = first % second;
|
||||||
first = temp;
|
first = temp;
|
||||||
}
|
}
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
function main() {
|
||||||
let first = 20;
|
let first = 20;
|
||||||
let second = 30;
|
let second = 30;
|
||||||
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
|
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
|
||||||
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
|
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
@ -1,31 +1,31 @@
|
|||||||
function sieveOfEratosthenes (n) {
|
function sieveOfEratosthenes(n) {
|
||||||
/*
|
/*
|
||||||
* Calculates prime numbers till a number n
|
* Calculates prime numbers till a number n
|
||||||
* :param n: Number upto which to calculate primes
|
* :param n: Number upto which to calculate primes
|
||||||
* :return: A boolean list contaning only primes
|
* :return: A boolean list contaning only primes
|
||||||
*/
|
*/
|
||||||
let primes = new Array(n + 1);
|
let primes = new Array(n + 1);
|
||||||
primes.fill(true); // set all as true initially
|
primes.fill(true); // set all as true initially
|
||||||
primes[0] = primes[1] = false; // Handling case for 0 and 1
|
primes[0] = primes[1] = false; // Handling case for 0 and 1
|
||||||
let sqrtn = Math.ceil(Math.sqrt(n));
|
let sqrtn = Math.ceil(Math.sqrt(n));
|
||||||
for (let i = 2; i <= sqrtn; i++) {
|
for (let i = 2; i <= sqrtn; i++) {
|
||||||
if (primes[i]) {
|
if (primes[i]) {
|
||||||
for (let j = 2 * i; j <= n; j += i) {
|
for (let j = 2 * i; j <= n; j += i) {
|
||||||
primes[j] = false;
|
primes[j] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return primes;
|
return primes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
function main() {
|
||||||
let n = 69; // number till where we wish to find primes
|
let n = 69; // number till where we wish to find primes
|
||||||
let primes = sieveOfEratosthenes(n);
|
let primes = sieveOfEratosthenes(n);
|
||||||
for (let i = 2; i <= n; i++) {
|
for (let i = 2; i <= n; i++) {
|
||||||
if (primes[i]) {
|
if (primes[i]) {
|
||||||
console.log(i);
|
console.log(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Caesar's Cipher - also known as the ROT13 Cipher is when
|
* Caesar's Cipher - also known as the ROT13 Cipher is when
|
||||||
* a letter is replaced by the one that is 13 spaces away
|
* a letter is replaced by the one that is 13 spaces away
|
||||||
* from it in the alphabet. If the letter is in the first half
|
* from it in the alphabet. If the letter is in the first half
|
||||||
* of the alphabet we add 13, if it's in the latter half we
|
* of the alphabet we add 13, if it's in the latter half we
|
||||||
* subtract 13 from the character code value.
|
* subtract 13 from the character code value.
|
||||||
@ -12,27 +12,27 @@
|
|||||||
* @return {String} decrypted string
|
* @return {String} decrypted string
|
||||||
*/
|
*/
|
||||||
function rot13(str) {
|
function rot13(str) {
|
||||||
let response = [];
|
let response = [];
|
||||||
let strLength = str.length;
|
let strLength = str.length;
|
||||||
|
|
||||||
for (let i =0; i < strLength; i++) {
|
for (let i = 0; i < strLength; i++) {
|
||||||
const char = str.charCodeAt(i);
|
const char = str.charCodeAt(i);
|
||||||
|
|
||||||
|
if (char < 65 || (char > 90 && char < 97) || char > 122) {
|
||||||
|
response.push(str.charAt(i));
|
||||||
|
} else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) {
|
||||||
|
response.push(String.fromCharCode(str.charCodeAt(i) - 13));
|
||||||
|
} else {
|
||||||
|
response.push(String.fromCharCode(str.charCodeAt(i) + 13));
|
||||||
|
}
|
||||||
|
|
||||||
if (char < 65 || (char > 90 && char < 97) || char > 122) {
|
|
||||||
response.push(str.charAt(i));
|
|
||||||
} else if ((char > 77 && char <= 90 ) || (char > 109 && char <= 122)) {
|
|
||||||
response.push(String.fromCharCode(str.charCodeAt(i) - 13));
|
|
||||||
} else {
|
|
||||||
response.push(String.fromCharCode(str.charCodeAt(i) + 13));
|
|
||||||
}
|
}
|
||||||
|
return response.join("");
|
||||||
}
|
|
||||||
return response.join('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Caesars Cipher Example
|
// Caesars Cipher Example
|
||||||
const encryptedString = 'Uryyb Jbeyq';
|
const encryptedString = "Uryyb Jbeyq";
|
||||||
const decryptedString = rot13(encryptedString);
|
const decryptedString = rot13(encryptedString);
|
||||||
|
|
||||||
console.log(decryptedString); // Hello World
|
console.log(decryptedString); // Hello World
|
||||||
|
@ -4,7 +4,7 @@ function decimalToBinary(num) {
|
|||||||
bin.unshift(num % 2);
|
bin.unshift(num % 2);
|
||||||
num >>= 1; // basically /= 2 without remainder if any
|
num >>= 1; // basically /= 2 without remainder if any
|
||||||
}
|
}
|
||||||
console.log("The decimal in binary is " + bin.join(''));
|
console.log("The decimal in binary is " + bin.join(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
decimalToBinary(2);
|
decimalToBinary(2);
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
/*Binary Search-Search a sorted array by repeatedly dividing the search interval
|
/*Binary Search-Search a sorted array by repeatedly dividing the search interval
|
||||||
* in half. Begin with an interval covering the whole array. If the value of the
|
* in half. Begin with an interval covering the whole array. If the value of the
|
||||||
* search key is less than the item in the middle of the interval, narrow the interval
|
* search key is less than the item in the middle of the interval, narrow the interval
|
||||||
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
|
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
|
||||||
* value is found or the interval is empty.
|
* value is found or the interval is empty.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function binarySearch(arr, i) {
|
function binarySearch(arr, i) {
|
||||||
var mid = Math.floor(arr.length / 2);
|
var mid = Math.floor(arr.length / 2);
|
||||||
if (arr[mid] === i) {
|
if (arr[mid] === i) {
|
||||||
console.log('match', arr[mid], i);
|
console.log("match", arr[mid], i);
|
||||||
return arr[mid];
|
return arr[mid];
|
||||||
} else if (arr[mid] < i && arr.length > 1) {
|
} else if (arr[mid] < i && arr.length > 1) {
|
||||||
binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
|
binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
|
||||||
} else if (arr[mid] > i && arr.length > 1) {
|
} else if (arr[mid] > i && arr.length > 1) {
|
||||||
binarySearch(arr.splice(0, mid), i);
|
binarySearch(arr.splice(0, mid), i);
|
||||||
} else {
|
} else {
|
||||||
console.log('not found', i);
|
console.log("not found", i);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var ar=[1,2,3,4,5,6,7,8,9,10];
|
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||||
binarySearch(ar,3);
|
binarySearch(ar, 3);
|
||||||
binarySearch(ar,7);
|
binarySearch(ar, 7);
|
||||||
binarySearch(ar,13);
|
binarySearch(ar, 13);
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
/*
|
/*
|
||||||
* Linear search or sequential search is a method for finding a target
|
* Linear search or sequential search is a method for finding a target
|
||||||
* value within a list. It sequentially checks each element of the list
|
* value within a list. It sequentially checks each element of the list
|
||||||
* for the target value until a match is found or until all the elements
|
* for the target value until a match is found or until all the elements
|
||||||
* have been searched.
|
* have been searched.
|
||||||
*/
|
*/
|
||||||
function SearchArray(searchNum, ar) {
|
function SearchArray(searchNum, ar) {
|
||||||
var position = Search(ar, searchNum);
|
var position = Search(ar, searchNum);
|
||||||
if (position != -1) {
|
if (position != -1) {
|
||||||
console.log("The element was found at " + (position + 1));
|
console.log("The element was found at " + (position + 1));
|
||||||
} else {
|
} else {
|
||||||
console.log("The element not found");
|
console.log("The element not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search “theArray” for the specified “key” value
|
// Search “theArray” for the specified “key” value
|
||||||
function Search(theArray, key) {
|
function Search(theArray, key) {
|
||||||
for (var n = 0; n < theArray.length; n++)
|
for (var n = 0; n < theArray.length; n++)
|
||||||
if (theArray[n] == key)
|
if (theArray[n] == key)
|
||||||
return n;
|
return n;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
@ -1,48 +1,48 @@
|
|||||||
/*
|
/*
|
||||||
* A simple helper function that checks, if the array is
|
* A simple helper function that checks, if the array is
|
||||||
* sorted in ascending order.
|
* sorted in ascending order.
|
||||||
*/
|
*/
|
||||||
Array.prototype.isSorted = function() {
|
Array.prototype.isSorted = function () {
|
||||||
|
|
||||||
let length = this.length;
|
let length = this.length;
|
||||||
|
|
||||||
if (length < 2) {
|
if (length < 2) {
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < length - 1; i++) {
|
|
||||||
if (this[i] > this[i + 1]) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
for (let i = 0; i < length - 1; i++) {
|
||||||
|
if (this[i] > this[i + 1]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A simple helper function to shuffle the array randomly in place.
|
* A simple helper function to shuffle the array randomly in place.
|
||||||
*/
|
*/
|
||||||
Array.prototype.shuffle = function() {
|
Array.prototype.shuffle = function () {
|
||||||
|
|
||||||
for (let i = this.length -1; i; i--) {
|
for (let i = this.length - 1; i; i--) {
|
||||||
let m = Math.floor(Math.random() * i);
|
let m = Math.floor(Math.random() * i);
|
||||||
let n = this[i - 1];
|
let n = this[i - 1];
|
||||||
this[i - 1] = this[m];
|
this[i - 1] = this[m];
|
||||||
this[m] = n;
|
this[m] = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Implementation of the bogosort algorithm. This sorting algorithm randomly
|
* Implementation of the bogosort algorithm. This sorting algorithm randomly
|
||||||
* rearranges the array until it is sorted.
|
* rearranges the array until it is sorted.
|
||||||
* For more information see: https://en.wikipedia.org/wiki/Bogosort
|
* For more information see: https://en.wikipedia.org/wiki/Bogosort
|
||||||
*/
|
*/
|
||||||
function bogoSort(items) {
|
function bogoSort(items) {
|
||||||
|
|
||||||
while(!items.isSorted()){
|
while (!items.isSorted()) {
|
||||||
items.shuffle()
|
items.shuffle()
|
||||||
}
|
}
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Implementation of bogoSort
|
//Implementation of bogoSort
|
||||||
@ -52,4 +52,4 @@ var ar = [5, 6, 7, 8, 1, 2, 12, 14];
|
|||||||
console.log(ar);
|
console.log(ar);
|
||||||
bogoSort(ar);
|
bogoSort(ar);
|
||||||
//Array after sort
|
//Array after sort
|
||||||
console.log(ar);
|
console.log(ar);
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
* Wiggle sort sorts the array into a wave like array.
|
* Wiggle sort sorts the array into a wave like array.
|
||||||
* An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
|
* An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= …..
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Array.prototype.wiggleSort = function() {
|
Array.prototype.wiggleSort = function () {
|
||||||
for (let i = 0; i < this.length; ++i) {
|
for (let i = 0; i < this.length; ++i) {
|
||||||
const shouldNotBeLessThan = i % 2;
|
const shouldNotBeLessThan = i % 2;
|
||||||
const isLessThan = this[i] < this[i + 1];
|
const isLessThan = this[i] < this[i + 1];
|
||||||
if (shouldNotBeLessThan && isLessThan) {
|
if (shouldNotBeLessThan && isLessThan) {
|
||||||
[this[i], this[i + 1]] = [this[i + 1], this[i]];
|
[this[i], this[i + 1]] = [this[i + 1], this[i]];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return this;
|
||||||
return this;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//Implementation of wiggle sort
|
//Implementation of wiggle sort
|
||||||
|
Reference in New Issue
Block a user