mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-09 05:35:45 +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,25 +1,25 @@
|
||||
function euclideanGCDRecursive (first, second) {
|
||||
function euclideanGCDRecursive(first, second) {
|
||||
/*
|
||||
Calculates GCD of two numbers using Euclidean Recursive Algorithm
|
||||
:param first: First number
|
||||
:param second: Second number
|
||||
:return: GCD of the numbers
|
||||
*/
|
||||
if (second == 0) {
|
||||
if (second === 0) {
|
||||
return first;
|
||||
} else {
|
||||
return euclideanGCDRecursive(second, (first % second));
|
||||
}
|
||||
}
|
||||
|
||||
function euclideanGCDIterative (first, second) {
|
||||
function euclideanGCDIterative(first, second) {
|
||||
/*
|
||||
Calculates GCD of two numbers using Euclidean Iterative Algorithm
|
||||
:param first: First number
|
||||
:param second: Second number
|
||||
:return: GCD of the numbers
|
||||
*/
|
||||
while (second != 0) {
|
||||
while (second !== 0) {
|
||||
let temp = second;
|
||||
second = first % second;
|
||||
first = temp;
|
||||
@ -27,7 +27,7 @@ function euclideanGCDIterative (first, second) {
|
||||
return first;
|
||||
}
|
||||
|
||||
function main () {
|
||||
function main() {
|
||||
let first = 20;
|
||||
let second = 30;
|
||||
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
|
||||
|
@ -1,4 +1,4 @@
|
||||
function sieveOfEratosthenes (n) {
|
||||
function sieveOfEratosthenes(n) {
|
||||
/*
|
||||
* Calculates prime numbers till a number n
|
||||
* :param n: Number upto which to calculate primes
|
||||
@ -18,7 +18,7 @@ function sieveOfEratosthenes (n) {
|
||||
return primes;
|
||||
}
|
||||
|
||||
function main () {
|
||||
function main() {
|
||||
let n = 69; // number till where we wish to find primes
|
||||
let primes = sieveOfEratosthenes(n);
|
||||
for (let i = 2; i <= n; i++) {
|
||||
|
@ -15,24 +15,24 @@ function rot13(str) {
|
||||
let response = [];
|
||||
let strLength = str.length;
|
||||
|
||||
for (let i =0; i < strLength; i++) {
|
||||
for (let i = 0; i < strLength; 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)) {
|
||||
} 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
|
||||
const encryptedString = 'Uryyb Jbeyq';
|
||||
const encryptedString = "Uryyb Jbeyq";
|
||||
const decryptedString = rot13(encryptedString);
|
||||
|
||||
console.log(decryptedString); // Hello World
|
@ -4,7 +4,7 @@ function decimalToBinary(num) {
|
||||
bin.unshift(num % 2);
|
||||
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);
|
||||
|
@ -1,27 +1,27 @@
|
||||
/*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
|
||||
* 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
|
||||
* value is found or the interval is empty.
|
||||
*/
|
||||
* 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
|
||||
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
|
||||
* value is found or the interval is empty.
|
||||
*/
|
||||
|
||||
function binarySearch(arr, i) {
|
||||
var mid = Math.floor(arr.length / 2);
|
||||
if (arr[mid] === i) {
|
||||
console.log('match', arr[mid], i);
|
||||
console.log("match", arr[mid], i);
|
||||
return arr[mid];
|
||||
} else if (arr[mid] < i && arr.length > 1) {
|
||||
binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
|
||||
} else if (arr[mid] > i && arr.length > 1) {
|
||||
binarySearch(arr.splice(0, mid), i);
|
||||
} else {
|
||||
console.log('not found', i);
|
||||
console.log("not found", i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var ar=[1,2,3,4,5,6,7,8,9,10];
|
||||
binarySearch(ar,3);
|
||||
binarySearch(ar,7);
|
||||
binarySearch(ar,13);
|
||||
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
binarySearch(ar, 3);
|
||||
binarySearch(ar, 7);
|
||||
binarySearch(ar, 13);
|
||||
|
@ -1,9 +1,9 @@
|
||||
/*
|
||||
* Linear search or sequential search is a method for finding a target
|
||||
* 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
|
||||
* have been searched.
|
||||
*/
|
||||
* Linear search or sequential search is a method for finding a target
|
||||
* 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
|
||||
* have been searched.
|
||||
*/
|
||||
function SearchArray(searchNum, ar) {
|
||||
var position = Search(ar, searchNum);
|
||||
if (position != -1) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* A simple helper function that checks, if the array is
|
||||
* sorted in ascending order.
|
||||
*/
|
||||
Array.prototype.isSorted = function() {
|
||||
* A simple helper function that checks, if the array is
|
||||
* sorted in ascending order.
|
||||
*/
|
||||
Array.prototype.isSorted = function () {
|
||||
|
||||
let length = this.length;
|
||||
|
||||
@ -19,11 +19,11 @@ Array.prototype.isSorted = function() {
|
||||
};
|
||||
|
||||
/*
|
||||
* A simple helper function to shuffle the array randomly in place.
|
||||
*/
|
||||
Array.prototype.shuffle = function() {
|
||||
* A simple helper function to shuffle the array randomly in place.
|
||||
*/
|
||||
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 n = this[i - 1];
|
||||
this[i - 1] = this[m];
|
||||
@ -33,13 +33,13 @@ Array.prototype.shuffle = function() {
|
||||
};
|
||||
|
||||
/*
|
||||
* Implementation of the bogosort algorithm. This sorting algorithm randomly
|
||||
* rearranges the array until it is sorted.
|
||||
* For more information see: https://en.wikipedia.org/wiki/Bogosort
|
||||
*/
|
||||
* Implementation of the bogosort algorithm. This sorting algorithm randomly
|
||||
* rearranges the array until it is sorted.
|
||||
* For more information see: https://en.wikipedia.org/wiki/Bogosort
|
||||
*/
|
||||
function bogoSort(items) {
|
||||
|
||||
while(!items.isSorted()){
|
||||
while (!items.isSorted()) {
|
||||
items.shuffle()
|
||||
}
|
||||
return items;
|
||||
|
@ -1,10 +1,10 @@
|
||||
/*
|
||||
* 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] >= …..
|
||||
*
|
||||
*/
|
||||
* 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] >= …..
|
||||
*
|
||||
*/
|
||||
|
||||
Array.prototype.wiggleSort = function() {
|
||||
Array.prototype.wiggleSort = function () {
|
||||
for (let i = 0; i < this.length; ++i) {
|
||||
const shouldNotBeLessThan = i % 2;
|
||||
const isLessThan = this[i] < this[i + 1];
|
||||
|
Reference in New Issue
Block a user