-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground-examples.js
More file actions
81 lines (65 loc) · 2.41 KB
/
playground-examples.js
File metadata and controls
81 lines (65 loc) · 2.41 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
75
76
77
78
79
80
81
// ===========================
// PLAYGROUND EXAMPLES
// ===========================
// Try running this code to see how it works!
// You can modify any values and run the code again.
console.log("🎮 Welcome to the JavaScript Playground!");
// Example 1: Basic Variables and Operations
console.log("\n--- Example 1: Variables and Math ---");
let myAge = 25;
let myName = "Alex";
let currentYear = 2024;
let birthYear = currentYear - myAge;
console.log(`Hi, I'm ${myName}!`);
console.log(`I'm ${myAge} years old`);
console.log(`I was born in ${birthYear}`);
// Example 2: Array Operations
console.log("\n--- Example 2: Working with Arrays ---");
let fruits = ["apple", "banana", "orange", "grape"];
console.log("Original fruits:", fruits);
// Add a new fruit
fruits.push("mango");
console.log("After adding mango:", fruits);
// Remove the first fruit
let removedFruit = fruits.shift();
console.log(`Removed ${removedFruit}, remaining:`, fruits);
// Example 3: Simple Function
console.log("\n--- Example 3: Functions ---");
function greetUser(name, timeOfDay) {
return `Good ${timeOfDay}, ${name}! Hope you're having a great day!`;
}
let greeting = greetUser("JavaScript Learner", "morning");
console.log(greeting);
// Example 4: Object Creation
console.log("\n--- Example 4: Objects ---");
let student = {
name: "Emma",
grade: "A",
subjects: ["Math", "Science", "English"],
isGraduated: false
};
console.log("Student info:", student);
console.log(`${student.name} has a ${student.grade} grade`);
console.log("Subjects:", student.subjects.join(", "));
// Example 5: Loops and Conditions
console.log("\n--- Example 5: Loops and Logic ---");
for (let i = 1; i <= 5; i++) {
if (i % 2 === 0) {
console.log(`${i} is even`);
} else {
console.log(`${i} is odd`);
}
}
// Example 6: Random Number Game
console.log("\n--- Example 6: Random Number Generator ---");
let randomNumber = Math.floor(Math.random() * 10) + 1;
let guessNumber = 7; // Change this to your guess!
console.log(`Your guess: ${guessNumber}`);
console.log(`Random number: ${randomNumber}`);
if (guessNumber === randomNumber) {
console.log("🎉 Congratulations! You guessed correctly!");
} else {
console.log("😅 Try again! Better luck next time!");
}
console.log("\n✨ Feel free to modify any of these examples and run them again!");
console.log("💡 Try changing variable values, adding new functions, or creating your own code!");