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

Fine-tunings.

parent cfad21f3
......@@ -57,7 +57,7 @@
<div class="container-md task-container" id="task-5">
<div class="jumbotron">
<h1 class="display-4">Task 5</h1>
<p class="lead">Loops</p>
<p class="lead">Loops / Predator-Prey</p>
<hr class="my-4">
<p>In the final exercise of the course a predator-prey system is modeled using object oriented programming.
</p>
......@@ -131,12 +131,12 @@
</tbody>
</table>
<button type="button" class="btn btn-primary" id="btn-task-5">Calculate</button>
<p>
<div id="task-5-chart-area">
<div id="task-5-chart-area">
<p>
<div id="task-5-chart-div"></div>
</div>
</p>
</p>
<div id="task-5-log"></div>
</div>
</div>
</div>
......@@ -150,8 +150,7 @@
number is not shown to the player. Instead, the computer asks the user to guess the number. After a
guess the computer indicates whether the guess was correct, too small or too large. The user is
repeatedly asked to guess until the answer is correct. Then, the program should tell how many guesses
the user needed. For generating a random integer, you can use, e.g., Math.floor(low + Math.random() *
(high - low + 1));
the user needed.
</p>
<hr>
<div class="input-group mb-3">
......@@ -257,8 +256,7 @@
&nbsp;alert(sum);<br>
</font>
The rationale is that after adding 0.1 ten times, the value of the loop counter is 1 and the
loop should
end.
loop should end.
</p>
<hr>
<p>
......@@ -276,9 +274,7 @@
<hr class="my-4">
<p> Write a program that asks the user’s age. If the age is 1-122 years the program welcomes the user. If
the age is 123-150 years, the program notes that we have a new age record. If the age is less than 1 or
more than 150 years, the program states that the user seems to be lying. For user interaction you can
use those mentioned in javascript.info: alert and prompt. The latter one can be used as follows: let age
= parseInt(prompt("What is your age in years?")); You may assume that the user inputs an integer.
more than 150 years, the program states that the user seems to be lying.
</p>
<hr>
<div class="input-group mb-3">
......@@ -333,8 +329,8 @@
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
......
const guessMin = 1;
const guessMax = 100;
let target = -1;
let guessCount = 0;
function initTask4() {
target = getRandomInteger(guessMin, guessMax);
......@@ -31,6 +32,7 @@ $("#input-task-4").keyup(function () {
// Show the result
$("#btn-task-4").click(function () {
guessCount++;
let guess = +$("#input-task-4").val();
let statement = getStatementForGuess(guess, target);
if (statement == -1) {
......@@ -44,7 +46,7 @@ $("#btn-task-4").click(function () {
$("#alert-task-4-result").addClass("alert-warning");
}
else {
statementText = "Correct! You guessed it!";
statementText = `Correct! You found it with ${guessCount} guesses.`;
$("#alert-task-4-result").addClass("alert-success");
$("#alert-task-4-result").removeClass("alert-warning");
$("#btn-task-4").hide();
......
......@@ -12,6 +12,9 @@ function initTask5() {
$("#input-task-5-beta").val(beta_default);
$("#input-task-5-delta").val(delta_default);
$("#input-task-5-gamma").val(gamma_default);
$("#task-5-log").text("");
$("#task-5-chart-area").hide();
}
$(".input-task-5").keyup(function () {
......@@ -21,19 +24,23 @@ $(".input-task-5").keyup(function () {
$("#btn-task-5").click(function () {
const t0_default = 0;
const delta_t = 0.001;
const loggingInterval = delta_t * 100;
const simulatedTimeSpan = 20;
$("#task-5-log").text("");
$("#task-5-chart-area").hide();
let x = +$("#input-task-5-x").val();
let y = +$("#input-task-5-y").val();
let alpha = +$("#input-task-5-alpha").val();
let beta = +$("#input-task-5-beta").val();
let delta = +$("#input-task-5-delta").val();
let gamma = +$("#input-task-5-gamma").val();
drawPredatorPrey(t0_default, x, y, alpha, beta, delta, gamma, delta_t, simulatedTimeSpan);
drawPredatorPrey(t0_default, x, y, alpha, beta, delta, gamma, delta_t, simulatedTimeSpan, loggingInterval);
$("#task-5-chart-area").show();
})
function drawPredatorPrey(t0, x, y, alpha, beta, delta, gamma, delta_t, simulatedTimeSpan) {
function drawPredatorPrey(t0, x, y, alpha, beta, delta, gamma, delta_t, simulatedTimeSpan, loggingInterval) {
google.charts.load('current', { packages: ['corechart', 'line'] });
google.charts.setOnLoadCallback(drawLineColors);
......@@ -45,9 +52,9 @@ function drawPredatorPrey(t0, x, y, alpha, beta, delta, gamma, delta_t, simulate
data.addColumn('number', 'Predators');
// Load data series
let predatorPreyObj = new predatorPrey(t0, x, y, alpha, beta, delta, gamma);
let timePoints = predatorPreyObj.getTimeSeries(delta_t, simulatedTimeSpan);
data.addRows(timePoints);
let predatorPreyObj = new predatorPrey();
let timePoints = predatorPreyObj.getTimeSeries(t0, x, y, alpha, beta, delta, gamma, delta_t, simulatedTimeSpan, loggingInterval);
data.addRows(timePoints.timeSeries);
var options = {
hAxis: {
......@@ -59,36 +66,47 @@ function drawPredatorPrey(t0, x, y, alpha, beta, delta, gamma, delta_t, simulate
},
colors: ['#a52714', '#097138'],
'height': 600,
'legend':'bottom',
'chartArea': {'width': '90%', 'height': '80%'},
'legend': 'bottom',
'chartArea': { 'width': '90%', 'height': '80%' },
};
var chart = new google.visualization.LineChart(document.getElementById('task-5-chart-div'));
chart.draw(data, options);
$("#task-5-log").append(timePoints.log);
}
}
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) {
// This is the actual loop function of the exercise
getTimeSeries(t0, x0, y0, alpha, beta, delta, gamma, delta_t, until_t, loggingInterval) {
let timeSeries = [];
timeSeries.push([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.x * this.y - this.gamma * this.y);
this.x = xNew;
this.y = yNew;
timeSeries.push([t, this.x, this.y]);
let logText = this.appendLogs(timeSeries, t0, x0, y0, loggingInterval, loggingInterval );
let x = x0, y = y0;
let integerLoggingInterval = Math.round(loggingInterval / delta_t);
for (let integerTime = Math.round(t0 / delta_t) + 1; integerTime <= Math.round(until_t / delta_t); integerTime++) {
let t = integerTime * delta_t;
let xNew = x + delta_t * (alpha * x - beta * x * y);
let yNew = y + delta_t * (delta * x * y - gamma * y);
x = xNew;
y = yNew;
logText += this.appendLogs(timeSeries, t, x, y, integerTime, integerLoggingInterval);
}
return { timeSeries: timeSeries, log: logText };
}
appendLogs(timeSeries, t, x, y, integerTime, integerLoggingInterval) {
// To display the chart
timeSeries.push([t, x, y]);
let logLine = "";
if (integerTime % integerLoggingInterval == 0) {
// To log to console, as requested
console.log(t, x, y);
// To log on screen
logLine = `t: ${t.toFixed(1)} &nbsp;&nbsp;&nbsp;&nbsp; x: ${x.toFixed(2)} &nbsp;&nbsp;&nbsp;&nbsp; y: ${y.toFixed(2)}<br>`;
}
return timeSeries;
return logLine;
}
}
describe("Task 5", function () {
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));
let pp = new predatorPrey();
let timeSeries = pp.getTimeSeries(0, 1, 1, 2 / 3, 1 / 3, 1, 1, 0.001, 20, 0.1);
// Check for correct amount of data points for chart
assert.equal(timeSeries.timeSeries.length, 20 / 0.001 + 1);
// Check for correctness of first and last data points
for(let i = 0; i < timeSeries.timeSeries[0].length; i++) {
assert.equal(timeSeries.timeSeries[0][i], ([0, 1, 1])[i]);
assert.equal(timeSeries.timeSeries[timeSeries.timeSeries.length - 1][i],
([20, 0.7325295779068618, 3.2007655022911785])[i]);
}
// Check for correct amount of log items by counting the line breaks <br>
assert.equal((timeSeries.log.match(/br/g) || []).length, 20 / 0.1 + 1);
});
});
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