Skip to content

Adding JavaScript with HTML for presentation #4

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 4 commits into from
Sep 14, 2017
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
68 changes: 67 additions & 1 deletion chapters/computational_physics/verlet.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ Submitted by P. Mekhail

#### C

```
```c
// Submitted by Gathros
#include <stdio.h>

Expand Down Expand Up @@ -502,3 +502,69 @@ int main(){

}
```

### JavaScript

```html
<!DOCTYPE html>
<html>
<body>
<script>
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 stormer_verlet(pos, acc, dt){

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;

vel += acc*dt;
}

return time;

}

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;
}

return time;

}

document.write(verlet(5.0, -10, 0.01) + "<br>");
document.write(stormer_verlet(5.0, -10, 0.01) + "<br>");
document.write(velocity_verlet(5.0, -10, 0.01) + "<br>");
</script>
</body>
</html>
```
77 changes: 76 additions & 1 deletion chapters/fundamental_algorithms/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ main()

### C

```C:
```c
// Submitted by Gathros
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -597,3 +597,78 @@ namespace Tree_Traversal
}
}
```

#### JavaScript

```html
<!DOCTYPE html>
<html>
<body>
<script>
function create_tree(n, num_row, num_child){
n.ID = num_row;
n.children = [];
if(num_row == 0){
return;
}

for(var i = 0; i < num_child; ++i){
var child = new Object();
create_tree(child, num_row - 1, num_child);
n.children.push(child);
}
};

function DFS_recursive(n){
document.write(n.ID + "<br>");
if(n.children.length == 0){
return;
}
for(var i = 0; i < n.children.length; ++i){
DFS_recursive(n.children[i]);
}
}

function DFS_stack(n){
var s = [];
s.push(n);
var temp;

while(s.length > 0){
temp = s.pop();
document.write(temp.ID + "<br>");
for(var i = 0; i < temp.children.length; ++i){
if(temp.children.length == 0){
break;
}
s.push(temp.children[i]);
}
}
}

function BFS_queue(n){
var q = [];
q.push(n);
var temp;

while(q.length > 0){
temp = q.shift();
document.write(temp.ID + "<br>");
for(var i = 0; i < temp.children.length; ++i){
if(temp.children.length == 0){
break;
}
q.push(temp.children[i]);
}
}
}

var root = new Object();
create_tree(root, 3, 3);
//DFS_recursive(root);
//DFS_stack(root);
BFS_queue(root);
</script>
</body>
<html>
```