JavaScript/Primitive data types/Exercises
Appearance
Topic: Primitive Data Types
1. Write a script with the following steps (the 'power'-operator is '**'):
- Compute the value of 250. Store it in the variable
x1
. - Compute the value of 250 + 1. Store it in the variable
x2
. - Compare
x1
withx2
. Are they equal?
Click to see solution
"use strict";
const x1 = 2 ** 50;
const x2 = 2 ** 50 + 1;
alert(x1 == x2); // false
2. Repeat the previous exercise with 260 instead of 250.
Click to see solution
"use strict";
const x1 = 2 ** 60;
const x2 = 2 ** 60 + 1;
alert(x1 == x2); // true
3. Repeat the previous exercise with 1000 instead of 1.
Click to see solution
"use strict";
const x1 = 2 ** 60;
const x2 = 2 ** 60 + 1000;
alert(x1 == x2); // false
4. Discuss the results of exercises 1 - 3 with your colleague.
5. Write a script with the following steps:
- Compute the value of one divided by three. Store the result in the variable
x1
. - Show
x1
. - Multiply
x1
by three and store the result inx2
. - Show
x2
. Which value do you expect?
Click to see solution
"use strict";
const x1 = 1 / 3;
alert(x1);
const x2 = x1 * 3;
alert(x2);
6. Write a script that shows that 5 and 5.0 are identical values.
Click to see solution
"use strict";
const x1 = 5;
const x2 = 5.0;
alert(x1 == x2);
7. Write a script that
- asks the user how old he is:
prompt(...)
prompt()
always returns a string. To perform the calculation, it's necessary to convert the answer to numberparseInt()
,parseFloat()
,Number()
- if his answer is a numeric value, show how often his heart has beaten since his birth (approximately once per second)
- else, give a message like "Sorry. We need a numeric value to perform the computation."
Click to see solution
// a possible solution:
"use strict";
const answer = prompt("How old are you?"); // returns a string
const age = parseFloat(answer); // what's about parseInt() or Number() ?
if (Number.isNaN(age)) { // try other solutions!
alert("Sorry, we need a number to perform the calculation. You typed: '" + answer + "'");
} else {
// about one beat per second
const seconds = age * 365 * 24 * 60 * 60;
alert("Your heart has beaten (approximately) " + seconds + " times since your birth.");
}
8. Create a HTML page for the previous exercise and embed the solution into the HTML. You can refer to this example.
Click to see solution
<!DOCTYPE html>
<html>
<head>
<title>Seconds</title>
<script>
function go() {
"use strict";
let result;
const answer = document.getElementById("age").value;
const age = Number(answer);
if (Number.isNaN(age)) { // try other solutions!
result = "Sorry, we need a number to perform the calculation. You typed: '" + answer + "'";
} else {
// about one beat per second
const seconds = age * 365 * 24 * 60 * 60;
result = "Your heart has beaten (approximately) " + seconds + " times since your birth.";
}
document.getElementById("result").innerText = result;
}
</script>
</head>
<body>
<h1>How many times has your heart beaten?</h1>
<label>Your age: </label>
<input type="text" id="age">
<button type="button" onclick="go()">GO</button>
<p>
<div id="result">???</div>
</p>
</body>
</html>