Commit a6b89efa authored by Tapio Mäntysalo's avatar Tapio Mäntysalo
Browse files

Task5 now works as unit tests

parent 2732e264
......@@ -36,6 +36,7 @@
<a class="dropdown-item" href="#" id="dropdown-task-2">Task 2</a>
<a class="dropdown-item" href="#" id="dropdown-task-3">Task 3</a>
<a class="dropdown-item" href="#" id="dropdown-task-4">Task 4</a>
<a class="dropdown-item" href="#" id="dropdown-task-5">Task 5</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" id="dropdown-example">Example</a>
<a class="dropdown-item" href="#" id="dropdown-tests">Unit tests</a>
......@@ -49,6 +50,22 @@
<div id="mocha">Unit test results</div>
</div>
<!-- Task 5 -->
<div class="container-md task-container" id="task-5">
<div class="jumbotron">
<h1 class="display-4">Task 5</h1>
<p class="lead">Loops</p>
<hr class="my-4">
<p>In the final exercise of the course a predator-prey system is modeled using object oriented programming.
</p>
<hr>
<p>
<button type="button" class="btn btn-primary" id="btn-task-5">Answer</button>
</p>
<p id="card-task-5-result"></p>
</div>
</div>
<!-- Task 4 -->
<div class="container-md task-container" id="task-4">
<div class="jumbotron">
......@@ -269,28 +286,38 @@
$("#example").show();
break;
case "dropdown-task-1":
initTask1();
$("#task-1").show();
break;
case "dropdown-task-2":
initTask2();
$("#task-2").show();
break;
case "dropdown-task-3":
initTask3();
$("#task-3").show();
break;
case "dropdown-task-4":
initTask4();
$("#task-4").show();
break;
case "dropdown-task-5":
initTask5();
$("#task-5").show();
break;
case "dropdown-tests":
$("#unit-tests").show();
$("#mocha").text("");
mocha.setup('bdd'); // minimal setup
mocha.run();
$("#dropdown-tests").hide();
break;
}
});
</script>
<!-- Tasks -->
<script src="task5.js"></script>
<script src="task4.js"></script>
<script src="task3.js"></script>
<script src="task2.js"></script>
......@@ -304,6 +331,7 @@
<script src="test/task2_test.js"></script>
<script src="test/task3_test.js"></script>
<script src="test/task4_test.js"></script>
<script src="test/task5_test.js"></script>
</body>
</html>
\ No newline at end of file
$("#card-task-1-result").text("");
$('.input-task-1').val("0");
$("#input-task-1").keyup(function () {deleteAllButNumbers(this)});
function initTask1() {
$("#card-task-1-result").text("");
$('.input-task-1').val("0");
$("#input-task-1").keyup(function () { deleteAllButNumbers(this) });
}
// Show the result
$("#btn-task-1").click(function () {
......
$("#card-task-2-result").text("");
function initTask2() {
$("#card-task-2-result").text("");
}
// Show the result
$("#btn-task-2").click(function () {
......
$("#card-task-3-result").text("");
$(".input-task-3").val("2");
$("#input-task-3-2").val("4");
$("#alert-task-3").hide();
function initTask3() {
$("#card-task-3-result").text("");
$(".input-task-3").val("2");
$("#input-task-3-2").val("4");
$("#alert-task-3").hide();
}
5
// Input validation
$(".input-task-3").keyup(function () {
$("#card-task-3-result").text("");
......
......@@ -2,7 +2,6 @@ const guessMin = 1;
const guessMax = 100;
let target = -1;
function initTask4() {
target = getRandomInteger(guessMin, guessMax);
$("#card-task-4-result").text("");
......
class predatorPrey {
constructor(t0, x, y, alpha, beta, delta, gamma) {
this.t0 = t0;
this.x = x;
this.y = y;
this.alpha = alpha;
this.beta = beta;
this.delta = delta;
this.gamma = gamma;
}
getTimeSeries(delta_t, until_t) {
let timeSeries = [];
timeSeries.push(new timePoint(this.t0, this.x, this.y));
for (let t = this.t0 + delta_t; t <= until_t; t += delta_t) {
let xNew = this.x + delta_t * (this.alpha * this.x - this.beta * this.x * this.y);
let yNew = this.y + delta_t * (this.delta * this.y - this.gamma * this.y);
let tp = new timePoint(t, xNew, yNew);
this.x = xNew;
this.y = yNew;
timeSeries.push(tp);
}
return timeSeries;
}
}
class timePoint {
constructor(t, x, y) {
this.t = t;
this.x = x;
this.y = y;
}
}
\ No newline at end of file
describe("Task 5", function () {
it("Uses timePoint class and array", function () {
let tp = new timePoint(1, 2, 3); // This is not a very useful test :)
let tpArray = [];
tpArray.push(tp);
assert.equal(tpArray[0].t, 1);
assert.equal(tpArray[0].x, 2);
assert.equal(tpArray[0].y, 3);
});
it("Calculates predator-prey function", function () {
let pp = new predatorPrey(0, 1, 1, 2/3, 1/3, 1, 1);
let timeSeries = pp.getTimeSeries(0.1, 20);
timeSeries.forEach(e => console.log(e));
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment