Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d74afef

Browse files
authored
Merge pull request TheAlgorithms#138 from cclauss/standard
Apply the JavaScript Standard Style
2 parents e62ad2f + 856dc2f commit d74afef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2286
-2417
lines changed

‎Algorithms/EucledianGCD.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
function euclideanGCDRecursive(first, second) {
2-
/*
1+
function euclideanGCDRecursive(first, second) {
2+
/*
33
Calculates GCD of two numbers using Euclidean Recursive Algorithm
44
:param first: First number
55
:param second: Second number
66
:return: GCD of the numbers
77
*/
8-
if (second === 0) {
9-
return first;
10-
} else {
11-
return euclideanGCDRecursive(second, (first % second));
12-
}
8+
if (second === 0) {
9+
return first
10+
} else {
11+
return euclideanGCDRecursive(second, (first % second))
12+
}
1313
}
1414

15-
function euclideanGCDIterative(first, second) {
16-
/*
15+
function euclideanGCDIterative(first, second) {
16+
/*
1717
Calculates GCD of two numbers using Euclidean Iterative Algorithm
1818
:param first: First number
1919
:param second: Second number
2020
:return: GCD of the numbers
2121
*/
22-
while (second !== 0) {
23-
lettemp = second;
24-
second = first % second;
25-
first = temp;
26-
}
27-
return first;
22+
while (second !== 0) {
23+
consttemp = second
24+
second = first % second
25+
first = temp
26+
}
27+
return first
2828
}
2929

30-
function main() {
31-
letfirst = 20;
32-
letsecond = 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+
constfirst = 20
32+
constsecond = 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

37-
main();
37+
main()

‎Algorithms/KadaneAlgo.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
function KadaneAlgo (array) {
2-
let cummulativeSum = 0
3-
let maxSum = 0
4-
for (var i = 0; i < array.length; i++) {
5-
cummulativeSum = cummulativeSum + array[i]
6-
if(cummulativeSum < 0 ) {
7-
cummulativeSum = 0
8-
}
9-
if (maxSum < cummulativeSum) {
10-
maxSum = cummulativeSum
11-
}
2+
let cummulativeSum = 0
3+
let maxSum = 0
4+
for (var i = 0; i < array.length; i++) {
5+
cummulativeSum = cummulativeSum + array[i]
6+
if (cummulativeSum < 0) {
7+
cummulativeSum = 0
128
}
13-
return maxSum
14-
// This function returns largest sum contigous sum in a array
9+
if (maxSum < cummulativeSum) {
10+
maxSum = cummulativeSum
11+
}
12+
}
13+
return maxSum
14+
// This function returns largest sum contigous sum in a array
1515
}
16-
function main() {
17-
var myArray = [1,2,3,4,-6]
18-
var result = KadaneAlgo(myArray)
19-
console.log(result)
16+
function main() {
17+
var myArray = [1,2,3,4,-6]
18+
var result = KadaneAlgo(myArray)
19+
console.log(result)
2020
}
21-
main()
21+
main()

‎Algorithms/dynamic_programming/fibonacci.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
// of the input value n, it is exponential in the size of n as
99
// a function of the number of input bits
1010

11-
function fib(n) {
12-
var table = [];
13-
table.push(1);
14-
table.push(1);
15-
for (var i = 2; i < n; ++i) {
16-
table.push(table[i - 1] + table[i - 2]);
17-
}
18-
console.log("Fibonacci #%d = %d", n, table[n - 1]);
11+
function fib(n) {
12+
var table = []
13+
table.push(1)
14+
table.push(1)
15+
for (var i = 2; i < n; ++i) {
16+
table.push(table[i - 1] + table[i - 2])
17+
}
18+
console.log('Fibonacci #%d = %d', n, table[n - 1])
1919
}
2020

21-
fib(1);
22-
fib(2);
23-
fib(200);
24-
fib(5);
25-
fib(10);
21+
fib(1)
22+
fib(2)
23+
fib(200)
24+
fib(5)
25+
fib(10)

‎Algorithms/sieveOfEratosthenes.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
function sieveOfEratosthenes(n) {
2-
/*
1+
function sieveOfEratosthenes(n) {
2+
/*
33
* Calculates prime numbers till a number n
44
* :param n: Number upto which to calculate primes
55
* :return: A boolean list contaning only primes
66
*/
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-
}
7+
const 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+
const 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+
}
1716
}
18-
return primes;
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-
}
21+
function main () {
22+
const n = 69 // number till where we wish to find primes
23+
const primes = sieveOfEratosthenes(n)
24+
for (let i = 2; i <= n; i++) {
25+
if (primes[i]) {
26+
console.log(i)
2827
}
28+
}
2929
}
3030

31-
main();
31+
main()

‎Ciphers/caesarsCipher.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@
1111
* @param {String} str - string to be decrypted
1212
* @return {String} decrypted string
1313
*/
14-
function rot13(str) {
15-
letresponse = [];
16-
letstrLength = str.length;
14+
function rot13(str) {
15+
constresponse = []
16+
conststrLength = str.length
1717

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-
}
18+
for (let i = 0; i < strLength; i++) {
19+
const char = str.charCodeAt(i)
2820

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))
2927
}
30-
return response.join("");
28+
}
29+
return response.join('')
3130
}
3231

33-
3432
// Caesars Cipher Example
35-
const encryptedString = "Uryyb Jbeyq";
36-
const decryptedString = rot13(encryptedString);
33+
const encryptedString = 'Uryyb Jbeyq'
34+
const decryptedString = rot13(encryptedString)
3735

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /