-
Notifications
You must be signed in to change notification settings - Fork 0
/
me.html
77 lines (59 loc) · 3.42 KB
/
me.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!--
Note on the <script> tag:
JS scripts can either be contained in the <head>,
in the <body> (in which case make sure to place the script at the END of the body)
or in a seperate file (to integrate them, we use <script src="filename.js"></script> either in the <head> or <body>)
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About me</title>
<script>
function intro(){
// page that takes user input and displays informations about the user
const user = { //declaring a user object (it's common practice to declare an object with const)
firstName: prompt("What is your first name?"),
lastName: prompt("What is your last name?"),
age: parseInt(prompt("How old will you be at the end of the current year?")),
fullName: function() { //that's how we make a object function
return this.firstName + " " + this.lastName; //this refers to the field of the object the function is called on
},
yearOfBirth: function(){
//using const to create an object doesn't make the object's attributes immutable ! It just makes the variable identifer not reassignable.
const d = new Date(); //creating a Date() object using the new keyword
return d.getFullYear() - this.age;
}
}
// Usefull way of encapsulating variables in strings without using the + operator
console.log(`Your first name is ${user.firstName} and your last name is ${user.lastName}. You are ${user.age} years old.`);
// changing the content of the page (using innerHTML to write into an HTML element)
document.getElementById("firstname").innerHTML = `Nice to meet you ${user.firstName}!`;
document.getElementById("lastname").innerHTML = `Your last name is ${user.lastName} right ? That's a pretty cool last name!`;
document.getElementById("mirror").innerHTML = `Your full name in the mirror would look like that: "${reverse(user.fullName())}". Nice isn't it?`;
document.getElementById("age").innerHTML = `You are ${user.age} years old, which means you were born in ${user.yearOfBirth()} right?`;
buttonAppear();
}
function reverse(str){
return str.split("").reverse().join("");
}
// function that's triggered at the end of intro(), it changes the content of the span tag and adds a button in it.
// I used single quotes so that the double quotes inside the HTML statement doesn't need to be escaped.
function buttonAppear(){
document.getElementById("printButton").innerHTML = '<button type="button" onclick="window.print()">Print your informations</button>';
}
</script>
</head>
<body>
<h2>Hi! I'd like to get to know you!</h2>
<p id="firstname"></p>
<p id="lastname"></p>
<p id="mirror"></p>
<p id="age"></p>
<button id="button" type="button" onclick="intro(); this.innerHTML = 'Introduce yourself again'">Introduce yourself!</button>
<!-- using an invisible span tag with an id to make a button appear when we want to -->
<span id="printButton"></span>
</body>
</html>