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

moved deleteAllBuNumbers to util.js

parent 75a37b8e
......@@ -2,10 +2,7 @@ $("#card-example-result").text("");
$('.input-example').val("0");
$(".input-example").keyup(function () {
// Ensure only number are entered
let s = $(this).val();
s = s.replace(/\D/g, '');
$(this).val(+s);
deleteAllButNumbers(this);
// Calculate and display the result
let a = +$("#input-example-1").val();
......
......@@ -12,9 +12,57 @@
<body>
<div class="alert alert-primary" role="alert">
Excercises of week 1, Tapio Mäntysalo, 28.3.2020, FS2
</div>
</div>
<!-- Task 3 -->
<div class="container-md">
<div class="jumbotron">
<h1 class="display-4">Task 3</h1>
<p class="lead">Loops and functions</p>
<hr class="my-4">
<p> Write a function that computes and returns the sum of all integers in the range [lower, upper] that are
divisible by d1 or d2 (or both). Lower, upper, d1 and d2 are parameters of the function; you should not
assume that they have any fixed values. Performance does not matter (the problem can be solved without a
loop but it requires a bit more thinking). This exercise is a modified version of the following:
https://projecteuler.net/problem=1.
Note: You can use the remainder operator % (see javascript.info) to check whether a number is divisible
by another number: if n % d == 0, n is divisible by d.
</p>
<hr>
<table>
<tbody>
<tr>
<td>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">Enter a divider</span>
</div>
<input type="text" class="form-control input-task-3" placeholder="" id="input-task-3-1"
aria-label="positive-integer-1" aria-describedby="basic-addon1">
</div>
</td>
<td>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon2">Enter another divider</span>
</div>
<input type="text" class="form-control input-task-3" placeholder="" id="input-task-3-2"
aria-label="positive-integer-2" aria-describedby="basic-addon2">
</div>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<p>
<button type="button" class="btn btn-primary" id="btn-task-3">Calculate</button>
</p>
<p id="card-task-3-result"></p>
</div>
</div>
<!-- Task 2 -->
<!-- Task 2 -->
<div class="container-md">
<div class="jumbotron">
<h1 class="display-4">Task 2</h1>
......@@ -26,9 +74,9 @@
<font face="courier">
&nbsp;let sum = 0; <br>
&nbsp;for(let i = 0; i != 1; i += 0.1) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;sum += i;<br>
&nbsp;} <br>
&nbsp;alert(sum);<br>
&nbsp;&nbsp;&nbsp;&nbsp;sum += i;<br>
&nbsp;} <br>
&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
......@@ -118,9 +166,11 @@
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
<script src="task2.js"></script>
<script src="task1.js"></script>
<script src="task3.js"></script>
<script src="task2.js"></script>
<script src="task1.js"></script>
<script src="example.js"></script>
<script src="util.js"></script>
</body>
</html>
\ No newline at end of file
$("#card-task-1-result").text("");
$('.input-task-1').val("0");
// Ensure that only numbers are entered
$("#input-task-1").keyup(function () {
let s = $(this).val();
s = s.replace(/\D/g, '');
$(this).val(+s);
});
$("#input-task-1").keyup(function () {deleteAllButNumbers(this)});
// Show the result
$("#btn-task-1").click(function () {
......
$("#card-task-3-result").text("");
$(".input-task-3").val("0");
$(".input-task-3").keyup(function () {deleteAllButNumbers(this)});
// Show the result
$("#btn-task-3").click(function () {
let age = +$("#input-task-3").val();
let statement = getStatementForAge(age);
$("#card-task-3-result").text(statement);
});
function getStatementForAge(age) {
if (age >= 1 && age <= 122) {
return "Welcome!";
}
else if (age >= 123 && age <= 150) {
return "We have a new age record!";
}
else {
return "You seem to be lying.";
}
}
\ No newline at end of file
// Ensure that only numbers are entered
function deleteAllButNumbers(inputItem) {
let s = $(inputItem).val();
s = s.replace(/\D/g, '');
$(inputItem).val(+s);
};
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