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

task 3 now works

parent 5aa714fa
......@@ -29,13 +29,16 @@
by another number: if n % d == 0, n is divisible by d.
</p>
<hr>
<div class="alert alert-danger" role="alert" id="alert-task-3">
This is a danger alert—check it out!
</div>
<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>
<span class="input-group-text" id="basic-addon1">Lower limit</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">
......@@ -44,7 +47,7 @@
<td>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon2">Enter another divider</span>
<span class="input-group-text" id="basic-addon2">Higher limit</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">
......@@ -53,6 +56,28 @@
<td>
</td>
</tr>
<tr>
<td>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">Divider 1</span>
</div>
<input type="text" class="form-control input-task-3" placeholder="" id="input-task-3-3"
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">Divider 2</span>
</div>
<input type="text" class="form-control input-task-3" placeholder="" id="input-task-3-4"
aria-label="positive-integer-2" aria-describedby="basic-addon2">
</div>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<p>
......
$("#card-task-3-result").text("");
$(".input-task-3").val("0");
$(".input-task-3").val("2");
$("#input-task-3-2").val("4");
$("#alert-task-3").hide();
$(".input-task-3").keyup(function () {deleteAllButNumbers(this)});
// Input validation
$(".input-task-3").keyup(function () {
$("#card-task-3-result").text("");
deleteAllButNumbersAndLeadingMinus(this);
let inputs = getInputs();
validateAndAlertInputs(inputs);
});
// Show the result
// Show results
$("#btn-task-3").click(function () {
let inputs = getInputs();
let errorInfo = validateAndAlertInputs(inputs);
if (errorInfo == "") {
let sum1 = calculateSumOfInts(inputs.limit1, inputs.limit2, inputs.divider1, inputs.divider1);
let sum2 = calculateSumOfInts(inputs.limit1, inputs.limit2, inputs.divider2, inputs.divider2);
let sumBoth = calculateSumOfInts(inputs.limit1, inputs.limit2, inputs.divider1, inputs.divider2);
let resultText = getResults(inputs.divider1, inputs.divider2, sum1, sum2, sumBoth);
$("#card-task-3-result").text(resultText);
}
});
function getInputs() {
let inputs = {
limit1: +$("#input-task-3-1").val(),
limit2: +$("#input-task-3-2").val(),
divider1: +$("#input-task-3-3").val(),
divider2: +$("#input-task-3-4").val(),
};
return inputs;
}
function validateAndAlertInputs(inputs) {
let errorInfo = "";
if (inputs.limit1 >= inputs.limit2) errorInfo = "Higher limit must be larger than lower limit.";
if (inputs.divider1 == 0 || inputs.divider2 == 0) errorInfo = "Divider must be larger than 0.";
if (errorInfo === "") {
$("#alert-task-3").hide();
}
else {
$("#alert-task-3").text(errorInfo);
$("#alert-task-3").show();
}
return errorInfo;
}
function getResults(divider1, divider2, sum1, sum2, sumBoth) {
let resultText = `Sum of integers divisible by ${divider1} is ${sum1}.
Sum of integers divisible by ${divider2} is ${sum2}.
Sum of integers divisible by ${divider1} and ${divider2} is ${sumBoth}.`;
return resultText;
};
function calculateSumOfInts(limit1, limit2, divider1, divider2) {
let sum = 0;
for (let i = limit1; i <= limit2; i++) {
sum += i % divider1 == 0 && i % divider2 == 0 ? i : 0;
}
return sum;
}
$("").click(function () {
let age = +$("#input-task-3").val();
let statement = getStatementForAge(age);
$("#card-task-3-result").text(statement);
......
......@@ -6,3 +6,11 @@ function deleteAllButNumbers(inputItem) {
$(inputItem).val(+s);
};
// Ensure that only numbers are entered, but allow for leading minus sign
function deleteAllButNumbersAndLeadingMinus(inputItem) {
let s = $(inputItem).val();
let minus = s.charAt(0) === "-" ? "-" : "";
s = s.replace(/\D/g, '');
s = minus + s;
$(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