From 6846c4a3b3fa8314a61b4012f3b14947e6847656 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Sun, 7 Oct 2018 17:01:08 +0200 Subject: [PATCH 1/3] Improved JavaScript implementation of Euclidean --- .../code/javascript/euclidean_example.js | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/contents/euclidean_algorithm/code/javascript/euclidean_example.js b/contents/euclidean_algorithm/code/javascript/euclidean_example.js index ccbe88c2d..fbaf4bfcc 100644 --- a/contents/euclidean_algorithm/code/javascript/euclidean_example.js +++ b/contents/euclidean_algorithm/code/javascript/euclidean_example.js @@ -1,32 +1,31 @@ -function euclid_mod(a, b){ - a = Math.abs(a); - b = Math.abs(b); +function euclidMod(a, b) { + a = Math.abs(a); + b = Math.abs(b); - var temp; - while (b != 0){ - temp = b; - b = a%b; - a = temp; - } + let temp; + while (b !== 0) { + temp = b; + b = a % b; + a = temp; + } - return a; + return a; } -function euclid_sub(a, b){ - a = Math.abs(a); - b = Math.abs(b); +function euclidSub(a, b) { + a = Math.abs(a); + b = Math.abs(b); - while (a != b){ - if (a > b){ - a = a - b; - } - else{ - b = b - a; - } + while (a !== b) { + if (a > b) { + a -= a - b; + } else { + b = b - a; } + } - return a; + return a; } -console.log(euclid_mod(64*67, 64*81)); -console.log(euclid_sub(128*12, 128*77)); +console.log(euclidMod(64 * 67, 64 * 81)); +console.log(euclidSub(128 * 12, 128 * 77)); From 16147f592ad7e979aab9d51caad989063871bba5 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Sun, 7 Oct 2018 17:01:18 +0200 Subject: [PATCH 2/3] Improved JavaScript implementation of Verlet --- .../code/javascript/verlet.js | 87 ++++++++++--------- .../verlet_integration/verlet_integration.md | 6 +- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/contents/verlet_integration/code/javascript/verlet.js b/contents/verlet_integration/code/javascript/verlet.js index d68d8ec49..013a7fa3f 100644 --- a/contents/verlet_integration/code/javascript/verlet.js +++ b/contents/verlet_integration/code/javascript/verlet.js @@ -1,55 +1,56 @@ -function verlet(pos, acc, dt){ - - var prev_pos, temp_pos, time; - prev_pos = pos; - time = 0; - - while (pos > 0){ - time += dt; - temp_pos = pos; - pos = pos*2 - prev_pos + acc * dt * dt; - prev_pos = temp_pos; - } - - return time; - +function verlet(pos, acc, dt) { + let prevPos = pos; + let time = 0; + let tempPos; + + while (pos > 0) { + time += dt; + tempPos = pos; + pos = pos * 2 - prevPos + acc * dt * dt; + prevPos = tempPos; + } + + return time; } -function stormer_verlet(pos, acc, dt){ +function stormerVerlet(pos, acc, dt) { + let prevPos = pos; + let time = 0; + let vel = 0; + let tempPos; - var prev_pos, temp_pos, time, vel; - prev_pos = pos; - vel = 0; - time = 0; - while (pos > 0){ - time += dt; - temp_pos = pos; - pos = pos*2 - prev_pos + acc * dt * dt; - prev_pos = temp_pos; + while (pos > 0) { + time += dt; + tempPos = pos; + pos = pos * 2 - prevPos + acc * dt * dt; + prevPos = tempPos; - vel += acc*dt; - } - - return time; + vel += acc * dt; + } + return { time, vel }; } -function velocity_verlet(pos, acc, dt){ - - var time, vel; - vel = 0; - time = 0; - while (pos > 0){ - time += dt; - pos += vel*dt + 0.5*acc * dt * dt; - vel += acc*dt; - } +function velocityVerlet(pos, acc, dt) { + let time = 0; + let vel = 0; - return time; + while (pos > 0) { + time += dt; + pos += vel * dt + 0.5 * acc * dt * dt; + vel += acc * dt; + } + return { time, vel }; } -console.log(verlet(5.0, -10, 0.01)); -console.log(stormer_verlet(5.0, -10, 0.01)); -console.log(velocity_verlet(5.0, -10, 0.01)); +const time = verlet(5, -10, 0.01); +console.log(`Time for Verlet integration is: ${time}\n`); + +const stormer = stormerVerlet(5, -10, 0.01); +console.log(`Time for Stormer Verlet integration is: ${stormer.time}`); +console.log(`Velocity for Stormer Verlet integration is: ${stormer.vel}\n`); +const velocity = velocityVerlet(5, -10, 0.01); +console.log(`Time for Velocity Verlet integration is: ${velocity.time}`); +console.log(`Velocity for Velocity Verlet integration is: ${velocity.vel}\n`); diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index fcdc6a1a6..1e555760a 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -52,7 +52,7 @@ Unfortunately, this has not yet been implemented in matlab, so here's Julia code Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code: [import:1-13, lang:"julia"](code/julia/verlet.jl) {% sample lang="javascript" %} -[import:1-16, lang:"javascript"](code/javascript/verlet.js) +[import:1-14, lang:"javascript"](code/javascript/verlet.js) {% sample lang="rs" %} [import:1-13, lang:"rust"](code/rust/verlet.rs) {% sample lang="swift" %} @@ -98,7 +98,7 @@ Unfortunately, this has not yet been implemented in matlab, so here's Julia code Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code: [import:15-31, lang:"julia"](code/julia/verlet.jl) {% sample lang="javascript" %} -[import:18-35, lang:"javascript"](code/javascript/verlet.js) +[import:16-32, lang:"javascript"](code/javascript/verlet.js) {% sample lang="rs" %} [import:15-32, lang:"rust"](code/rust/verlet.rs) {% sample lang="swift" %} @@ -158,7 +158,7 @@ Unfortunately, this has not yet been implemented in matlab, so here's Julia code Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code: [import:33-45, lang:"julia"](code/julia/verlet.jl) {% sample lang="javascript" %} -[import:37-50, lang:"javascript"](code/javascript/verlet.js) +[import:34-45, lang:"javascript"](code/javascript/verlet.js) {% sample lang="rs" %} [import:34-45, lang:"rust"](code/rust/verlet.rs) {% sample lang="swift" %} From f484ee47e11565bcb56d24791e8ff03502f42958 Mon Sep 17 00:00:00 2001 From: Marius Becker Date: Sun, 7 Oct 2018 17:19:40 +0200 Subject: [PATCH 3/3] Improved the JS implementation for Bubble Sort --- contents/bubble_sort/bubble_sort.md | 2 +- contents/bubble_sort/code/javascript/bubble.js | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index e64793bdf..1e1d0fe57 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -19,7 +19,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with {% sample lang="kotlin" %} [import:1-11, lang:"kotlin"](code/kotlin/BubbleSort.kt) {% sample lang="js" %} -[import:1-11, lang:"javascript"](code/javascript/bubble.js) +[import:1-12, lang:"javascript"](code/javascript/bubble.js) {% sample lang="py" %} [import:4-9, lang:"python"](code/python/bubblesort.py) {% sample lang="m" %} diff --git a/contents/bubble_sort/code/javascript/bubble.js b/contents/bubble_sort/code/javascript/bubble.js index c4f57bb8e..f1f36d971 100644 --- a/contents/bubble_sort/code/javascript/bubble.js +++ b/contents/bubble_sort/code/javascript/bubble.js @@ -1,17 +1,18 @@ function bubbleSort(arr) { - for (let r = arr.length -1; r > 0; r--) { - for (let i = 0; i < r; i++) { - if (arr[i] > arr[i + 1]) { - let tmp = arr[i]; - arr[i] = arr[i + 1]; - arr[i + 1] = tmp; + let tmp; + for (let i = 0; i < arr.length; i++) { + for (let k = 0; k < arr.length - 1; k++) { + if (arr[k] > arr[k + 1]) { + tmp = arr[k]; + arr[k] = arr[k + 1]; + arr[k + 1] = tmp; } } } } function main() { - const testArray = [4, 5, 123, 759, -132, 8940, 24, 34, -5]; + const testArray = [1, 3, 2, 4, 5, 10, 50, 7, 1.5, 0.3]; bubbleSort(testArray); console.log(testArray); }