-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (56 loc) · 2.14 KB
/
index.js
File metadata and controls
74 lines (56 loc) · 2.14 KB
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
// ===================================
// JavaScript User Input - Two Methods
// ===================================
// METHOD 1: Window Prompt (Easy Way)
// -----------------------------------
// Uncomment to try the window prompt method:
// let username;
// username = window.prompt("What's your username?");
// console.log(username);
// Alternative: Combined declaration and assignment
// window.alert("Welcome to the site!"); // Greet user with alert
// let username = window.prompt("What's your username?");
// console.log(username);
// document.getElementById("myHeading").textContent = `Hello ${username}`;
// document.getElementById("myParagraph").textContent = `Welcome to the website, ${username}!`;
// METHOD 2: HTML Text Box (Professional Way)
// -------------------------------------------
let username;
document.getElementById("mySubmit").onclick = function() {
// Get value from text box
username = document.getElementById("userName").value;
console.log(username);
// Update H1 element with greeting
document.getElementById("myH1").textContent = `Welcome, ${username}`;
// Also log to console
console.log(`Username entered: ${username}`);
};
// ===================================
// Practice Examples
// ===================================
// Uncomment to try these exercises:
// Exercise 1: Age Calculator
/*
let birthYear = window.prompt("What year were you born?");
birthYear = Number(birthYear);
let currentYear = new Date().getFullYear();
let age = currentYear - birthYear;
console.log(`You are ${age} years old!`);
*/
// Exercise 2: Greeting with First and Last Name
/*
let firstName = window.prompt("Enter your first name:");
let lastName = window.prompt("Enter your last name:");
console.log(`Hello ${firstName} ${lastName}!`);
*/
// Exercise 3: Form Validation
/*
document.getElementById("mySubmit").onclick = function() {
username = document.getElementById("myText").value;
if (username === "" || username === null) {
document.getElementById("myH1").textContent = "Please enter a username!";
} else {
document.getElementById("myH1").textContent = `Hello ${username}`;
}
};
*/