Merge pull request #88 from PatOnTheBack/master

Improved JSLint Compliance and Created Math Algorithms
This commit is contained in:
Yang Libin
2019-08-07 18:33:05 +08:00
committed by GitHub
12 changed files with 289 additions and 143 deletions

View File

@ -5,7 +5,7 @@ function euclideanGCDRecursive (first, second) {
:param second: Second number
:return: GCD of the numbers
*/
if (second == 0) {
if (second === 0) {
return first;
} else {
return euclideanGCDRecursive(second, (first % second));
@ -19,7 +19,7 @@ function euclideanGCDIterative (first, second) {
:param second: Second number
:return: GCD of the numbers
*/
while (second != 0) {
while (second !== 0) {
let temp = second;
second = first % second;
first = temp;

View File

@ -27,12 +27,12 @@ function rot13(str) {
}
}
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

View File

@ -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);

View File

@ -8,14 +8,14 @@
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;
}

26
maths/abs.js Normal file
View File

@ -0,0 +1,26 @@
/*
author: PatOnTheBack
license: GPL-3.0 or later
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/abs.py
This script will find the absolute value of a number.
More about absolute values:
https://en.wikipedia.org/wiki/Absolute_value
*/
function abs_val(num) {
// Find absolute value of `num`.
"use strict";
if (num < 0) {
return -num
}
// Executes if condition is not met.
return num
}
// Run `abs` function to find absolute value of two numbers.
console.log("The absolute value of -34 is " + abs_val(-34));
console.log("The absolute value of 34 is " + abs_val(34));

30
maths/average_mean.js Normal file
View File

@ -0,0 +1,30 @@
/*
author: PatOnTheBack
license: GPL-3.0 or later
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/average.py
This script will find the average (mean) of an array of numbers.
More about mean:
https://en.wikipedia.org/wiki/Mean
*/
function mean(nums) {
"use strict";
var sum = 0;
var avg;
// This loop sums all values in the 'nums' array.
nums.forEach(function (current) {
sum += current;
});
// Divide sum by the length of the 'nums' array.
avg = sum / nums.length;
return avg;
}
// Run `mean` Function to find average of a list of numbers.
console.log(mean([2, 4, 6, 8, 20, 50, 70]));

52
maths/factorial.js Normal file
View File

@ -0,0 +1,52 @@
/*
author: PatOnTheBack
license: GPL-3.0 or later
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py
This script will find the factorial of a number provided by the user.
More about factorials:
https://en.wikipedia.org/wiki/factorial
*/
"use strict";
function calc_range(num) {
// Generate a range of numbers from 1 to `num`.
var i = 1;
var range = [];
while (i <= num) {
range.push(i);
i += 1;
}
return range;
}
function calc_factorial(num) {
var factorial;
var range = calc_range(num);
// Check if the number is negative, positive, null, undefined, or zero
if (num < 0) {
return "Sorry, factorial does not exist for negative numbers.";
}
if (num === null || num === undefined) {
return "Sorry, factorial does not exist for null or undefined numbers.";
}
if (num === 0) {
return "The factorial of 0 is 1.";
}
if (num > 0) {
factorial = 1;
range.forEach(function (i) {
factorial = factorial * i;
});
return "The factorial of " + num + " is " + factorial;
}
}
// Run `factorial` Function to find average of a list of numbers.
var num = prompt("Enter a number: ");
alert(calc_factorial(num));

38
maths/find_lcm.js Normal file
View File

@ -0,0 +1,38 @@
/*
author: PatOnTheBack
license: GPL-3.0 or later
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py
More about LCM:
https://en.wikipedia.org/wiki/Least_common_multiple
*/
"use strict";
// Find the LCM of two numbers.
function find_lcm(num_1, num_2) {
var max_num;
var lcm;
// Check to see whether num_1 or num_2 is larger.
if (num_1 > num_2) {
max_num = num_1;
} else {
max_num = num_2;
}
lcm = max_num;
while (true) {
if ((lcm % num_1 === 0) && (lcm % num_2 === 0)) {
break;
}
lcm += max_num;
}
return lcm;
}
// Run `find_lcm` Function
var num_1 = 12;
var num_2 = 76;
console.log(find_lcm(num_1, num_2));