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

Task-1 works now

parent 2e2a4c94
$("#card-example-result").text(""); $("#card-example-result").text("");
$('.input-example').val("0"); $('.input-example').val("0");
$(".input-example").keyup(function () { $(".input-example").keyup(function () {
// Ensure only number are entered
let s = $(this).val(); let s = $(this).val();
s = s.replace(/\D/g, ''); s = s.replace(/\D/g, '');
$(this).val(+s); $(this).val(+s);
// });
// $("#btn-example").click(function () { // Calculate and display the result
let a = +$("#input-example-1").val(); let a = +$("#input-example-1").val();
let b = +$("#input-example-2").val(); let b = +$("#input-example-2").val();
let gcd = getGreatestCommonDivisor(a, b); let gcd = getGreatestCommonDivisor(a, b);
......
...@@ -10,6 +10,33 @@ ...@@ -10,6 +10,33 @@
</head> </head>
<body> <body>
<!-- Task 1 -->
<div class="container-md">
<div class="jumbotron">
<h1 class="display-4">Task 1</h1>
<p class="lead">Conditional statements</p>
<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.
</p>
<hr>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">What is your age in years?</span>
</div>
<input type="text" class="form-control input-task-1" placeholder="" id="input-task-1"
aria-label="positive-integer-1" aria-describedby="basic-addon1">
</div>
<p>
<button type="button" class="btn btn-primary" id="btn-task-1">Answer</button>
</p>
<p id="card-task-1-result"></p>
</div>
</div>
<!-- Example --> <!-- Example -->
<div class="container-md"> <div class="container-md">
<div class="jumbotron"> <div class="jumbotron">
...@@ -57,7 +84,8 @@ ...@@ -57,7 +84,8 @@
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script src="new_example.js"></script> <script src="task1.js"></script>
<script src="example.js"></script>
</body> </body>
</html> </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);
});
// Show the result
$("#btn-task-1").click(function () {
let age = +$("#input-task-1").val();
let statement = getStatementForAge(age);
$("#card-task-1-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
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