Skip to content

Improved some JavaScript examples #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down
15 changes: 8 additions & 7 deletions contents/bubble_sort/code/javascript/bubble.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
45 changes: 22 additions & 23 deletions contents/euclidean_algorithm/code/javascript/euclidean_example.js
Original file line number Diff line number Diff line change
@@ -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));
87 changes: 44 additions & 43 deletions contents/verlet_integration/code/javascript/verlet.js
Original file line number Diff line number Diff line change
@@ -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`);
6 changes: 3 additions & 3 deletions contents/verlet_integration/verlet_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down