Skip to content

Commit 7123a3c

Browse files
Merge pull request #480 from algorithm-archivists/js-formatting
Improved some JavaScript examples
2 parents cbf5084 + f484ee4 commit 7123a3c

File tree

5 files changed

+78
-77
lines changed

5 files changed

+78
-77
lines changed

contents/bubble_sort/bubble_sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1919
{% sample lang="kotlin" %}
2020
[import:1-11, lang:"kotlin"](code/kotlin/BubbleSort.kt)
2121
{% sample lang="js" %}
22-
[import:1-11, lang:"javascript"](code/javascript/bubble.js)
22+
[import:1-12, lang:"javascript"](code/javascript/bubble.js)
2323
{% sample lang="py" %}
2424
[import:4-9, lang:"python"](code/python/bubblesort.py)
2525
{% sample lang="m" %}

contents/bubble_sort/code/javascript/bubble.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
function bubbleSort(arr) {
2-
for (let r = arr.length -1; r > 0; r--) {
3-
for (let i = 0; i < r; i++) {
4-
if (arr[i] > arr[i + 1]) {
5-
let tmp = arr[i];
6-
arr[i] = arr[i + 1];
7-
arr[i + 1] = tmp;
2+
let tmp;
3+
for (let i = 0; i < arr.length; i++) {
4+
for (let k = 0; k < arr.length - 1; k++) {
5+
if (arr[k] > arr[k + 1]) {
6+
tmp = arr[k];
7+
arr[k] = arr[k + 1];
8+
arr[k + 1] = tmp;
89
}
910
}
1011
}
1112
}
1213

1314
function main() {
14-
const testArray = [4, 5, 123, 759, -132, 8940, 24, 34, -5];
15+
const testArray = [1, 3, 2, 4, 5, 10, 50, 7, 1.5, 0.3];
1516
bubbleSort(testArray);
1617
console.log(testArray);
1718
}
Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
function euclid_mod(a, b){
2-
a = Math.abs(a);
3-
b = Math.abs(b);
1+
function euclidMod(a, b) {
2+
a = Math.abs(a);
3+
b = Math.abs(b);
44

5-
var temp;
6-
while (b != 0){
7-
temp = b;
8-
b = a%b;
9-
a = temp;
10-
}
5+
let temp;
6+
while (b !== 0) {
7+
temp = b;
8+
b = a % b;
9+
a = temp;
10+
}
1111

12-
return a;
12+
return a;
1313
}
1414

15-
function euclid_sub(a, b){
16-
a = Math.abs(a);
17-
b = Math.abs(b);
15+
function euclidSub(a, b) {
16+
a = Math.abs(a);
17+
b = Math.abs(b);
1818

19-
while (a != b){
20-
if (a > b){
21-
a = a - b;
22-
}
23-
else{
24-
b = b - a;
25-
}
19+
while (a !== b) {
20+
if (a > b) {
21+
a -= a - b;
22+
} else {
23+
b = b - a;
2624
}
25+
}
2726

28-
return a;
27+
return a;
2928
}
3029

31-
console.log(euclid_mod(64*67, 64*81));
32-
console.log(euclid_sub(128*12, 128*77));
30+
console.log(euclidMod(64 * 67, 64 * 81));
31+
console.log(euclidSub(128 * 12, 128 * 77));
Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
function verlet(pos, acc, dt){
2-
3-
var prev_pos, temp_pos, time;
4-
prev_pos = pos;
5-
time = 0;
6-
7-
while (pos > 0){
8-
time += dt;
9-
temp_pos = pos;
10-
pos = pos*2 - prev_pos + acc * dt * dt;
11-
prev_pos = temp_pos;
12-
}
13-
14-
return time;
15-
1+
function verlet(pos, acc, dt) {
2+
let prevPos = pos;
3+
let time = 0;
4+
let tempPos;
5+
6+
while (pos > 0) {
7+
time += dt;
8+
tempPos = pos;
9+
pos = pos * 2 - prevPos + acc * dt * dt;
10+
prevPos = tempPos;
11+
}
12+
13+
return time;
1614
}
1715

18-
function stormer_verlet(pos, acc, dt){
16+
function stormerVerlet(pos, acc, dt) {
17+
let prevPos = pos;
18+
let time = 0;
19+
let vel = 0;
20+
let tempPos;
1921

20-
var prev_pos, temp_pos, time, vel;
21-
prev_pos = pos;
22-
vel = 0;
23-
time = 0;
24-
while (pos > 0){
25-
time += dt;
26-
temp_pos = pos;
27-
pos = pos*2 - prev_pos + acc * dt * dt;
28-
prev_pos = temp_pos;
22+
while (pos > 0) {
23+
time += dt;
24+
tempPos = pos;
25+
pos = pos * 2 - prevPos + acc * dt * dt;
26+
prevPos = tempPos;
2927

30-
vel += acc*dt;
31-
}
32-
33-
return time;
28+
vel += acc * dt;
29+
}
3430

31+
return { time, vel };
3532
}
3633

37-
function velocity_verlet(pos, acc, dt){
38-
39-
var time, vel;
40-
vel = 0;
41-
time = 0;
42-
while (pos > 0){
43-
time += dt;
44-
pos += vel*dt + 0.5*acc * dt * dt;
45-
vel += acc*dt;
46-
}
34+
function velocityVerlet(pos, acc, dt) {
35+
let time = 0;
36+
let vel = 0;
4737

48-
return time;
38+
while (pos > 0) {
39+
time += dt;
40+
pos += vel * dt + 0.5 * acc * dt * dt;
41+
vel += acc * dt;
42+
}
4943

44+
return { time, vel };
5045
}
5146

52-
console.log(verlet(5.0, -10, 0.01));
53-
console.log(stormer_verlet(5.0, -10, 0.01));
54-
console.log(velocity_verlet(5.0, -10, 0.01));
47+
const time = verlet(5, -10, 0.01);
48+
console.log(`Time for Verlet integration is: ${time}\n`);
49+
50+
const stormer = stormerVerlet(5, -10, 0.01);
51+
console.log(`Time for Stormer Verlet integration is: ${stormer.time}`);
52+
console.log(`Velocity for Stormer Verlet integration is: ${stormer.vel}\n`);
5553

54+
const velocity = velocityVerlet(5, -10, 0.01);
55+
console.log(`Time for Velocity Verlet integration is: ${velocity.time}`);
56+
console.log(`Velocity for Velocity Verlet integration is: ${velocity.vel}\n`);

contents/verlet_integration/verlet_integration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Unfortunately, this has not yet been implemented in matlab, so here's Julia code
5252
Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code:
5353
[import:1-13, lang:"julia"](code/julia/verlet.jl)
5454
{% sample lang="javascript" %}
55-
[import:1-16, lang:"javascript"](code/javascript/verlet.js)
55+
[import:1-14, lang:"javascript"](code/javascript/verlet.js)
5656
{% sample lang="rs" %}
5757
[import:1-13, lang:"rust"](code/rust/verlet.rs)
5858
{% sample lang="swift" %}
@@ -98,7 +98,7 @@ Unfortunately, this has not yet been implemented in matlab, so here's Julia code
9898
Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code:
9999
[import:15-31, lang:"julia"](code/julia/verlet.jl)
100100
{% sample lang="javascript" %}
101-
[import:18-35, lang:"javascript"](code/javascript/verlet.js)
101+
[import:16-32, lang:"javascript"](code/javascript/verlet.js)
102102
{% sample lang="rs" %}
103103
[import:15-32, lang:"rust"](code/rust/verlet.rs)
104104
{% sample lang="swift" %}
@@ -158,7 +158,7 @@ Unfortunately, this has not yet been implemented in matlab, so here's Julia code
158158
Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code:
159159
[import:33-45, lang:"julia"](code/julia/verlet.jl)
160160
{% sample lang="javascript" %}
161-
[import:37-50, lang:"javascript"](code/javascript/verlet.js)
161+
[import:34-45, lang:"javascript"](code/javascript/verlet.js)
162162
{% sample lang="rs" %}
163163
[import:34-45, lang:"rust"](code/rust/verlet.rs)
164164
{% sample lang="swift" %}

0 commit comments

Comments
 (0)