Skip to content

Commit 10eed94

Browse files
authored
Add files via upload
1 parent a43bf2a commit 10eed94

23 files changed

Lines changed: 4817 additions & 0 deletions

1. JavaScript Introduction.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 1- Getting Started With JavaScript
2+
***
3+
JavaScript is not something you need to install separately like a software program; it's already built into modern web browsers. To create and run a basic JavaScript program, you only need a text editor and a web browser. Here's a simple guide to creating your first JavaScript program:
4+
5+
1. **Text Editor:**
6+
Choose a text editor to write your JavaScript code. You can use popular text editors like Visual Studio Code, Sublime Text, Atom, Notepad++, or even a plain text editor like Notepad on Windows or TextEdit on macOS.
7+
8+
2. **Create an HTML File:**
9+
Create an HTML file where you'll include your JavaScript code. You can do this by opening your text editor and creating a new file with a ".html" extension. For example, you can name it `index.html`.
10+
11+
3. **Write the HTML Structure:**
12+
In your `index.html` file, write the basic structure of an HTML document, including an opening and closing `<html>`, `<head>`, and `<body>` tags. Here's a simple example:
13+
14+
```html
15+
<!DOCTYPE html>
16+
<html>
17+
<head>
18+
<title>My First JavaScript Program</title>
19+
</head>
20+
<body>
21+
<!-- Your JavaScript code will go here -->
22+
</body>
23+
</html>
24+
```
25+
26+
4. **Adding JavaScript Code:**
27+
Inside the `<body>` section of your HTML file, you can include JavaScript code within `<script>` tags. You can place the `<script>` tags in the `<head>` or at the end of the `<body>` section. Here's an example of adding a basic JavaScript program that displays a message:
28+
29+
```html
30+
<!DOCTYPE html>
31+
<html>
32+
<head>
33+
<title>My First JavaScript Program</title>
34+
</head>
35+
<body>
36+
<script>
37+
// Your JavaScript code goes here
38+
alert('Hello, JavaScript!');
39+
</script>
40+
</body>
41+
</html>
42+
```
43+
44+
5. **Save the File:**
45+
Save your `index.html` file after adding the JavaScript code.
46+
47+
6. **Open in a Web Browser:**
48+
Locate your `index.html` file and open it with your preferred web browser (e.g., Google Chrome, Mozilla Firefox, or Microsoft Edge). You should see an alert message saying "Hello, JavaScript!"
49+
50+
That's it! You've created and executed a basic JavaScript program in an HTML document. As you continue to learn JavaScript, you can add more complex code and interact with the HTML document's content and structure. You can also link external JavaScript files and build interactive web applications as you become more proficient.

10. switch statement.md

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
# 10. switch Statement
2+
3+
In JavaScript, the `switch` statement is used for making decisions based on the value of an expression. It provides a way to compare a single value against multiple possible case values and execute code blocks based on which case value matches the value of the expression. Here's the basic syntax of the `switch` statement:
4+
5+
```javascript
6+
switch (expression) {
7+
case value1:
8+
// Code to execute if expression matches value1
9+
break;
10+
case value2:
11+
// Code to execute if expression matches value2
12+
break;
13+
// Additional cases can be added as needed.
14+
default:
15+
// Code to execute if none of the cases match the expression
16+
}
17+
```
18+
19+
Here's a breakdown of how it works:
20+
21+
1. The `switch` keyword is followed by an expression enclosed in parentheses. This expression is the value that you want to compare against the different case values.
22+
23+
2. Inside the `switch` block, you define one or more `case` blocks. Each `case` block specifies a value that you want to compare with the expression. If the expression matches a `case` value, the code inside that `case` block will be executed.
24+
25+
3. After each `case` block, you typically include a `break` statement. This `break` statement is used to exit the `switch` statement after executing the code in the matching `case`. Without a `break` statement, the `switch` statement would continue to execute the code in subsequent `case` blocks.
26+
27+
4. You can also include a `default` block at the end of the `switch` statement. The `default` block is executed if none of the `case` values match the expression.
28+
29+
Here's an example:
30+
31+
```javascript
32+
let day = "Monday";
33+
34+
switch (day) {
35+
case "Monday":
36+
console.log("It's the start of the workweek.");
37+
break;
38+
case "Friday":
39+
console.log("It's almost the weekend.");
40+
break;
41+
default:
42+
console.log("It's a regular day.");
43+
}
44+
```
45+
46+
In this example, the `switch` statement compares the value of the `day` variable against different cases and prints a message based on the value of `day`. Since `day` is "Monday" in this case, it will print "It's the start of the workweek."
47+
48+
The `switch` statement is useful when you have multiple conditions to check against a single expression, and it can provide a cleaner and more efficient way to handle these situations compared to using a series of `if/else if` statements.
49+
50+
## Example
51+
```js
52+
const number = parseInt(prompt('Enter a number between 1 to 7: '));
53+
54+
switch (number) {
55+
case 1:
56+
console.log("Sunday");
57+
break;
58+
case 2:
59+
console.log("Monday");
60+
break;
61+
case 3:
62+
console.log("Tuesday");
63+
break;
64+
case 4:
65+
console.log("Wednesday");
66+
break;
67+
case 5:
68+
console.log("Thursday");
69+
break;
70+
case 6:
71+
console.log("Friday");
72+
break;
73+
case 7:
74+
console.log("Saturday");
75+
break;
76+
default:
77+
console.log("Invalid Number");
78+
}
79+
```
80+
**Output**
81+
```
82+
Enter a number between 1 to 7: 4
83+
Wednesday
84+
```
85+
***
86+
## Omitting break
87+
```js
88+
const number = parseInt(prompt('Enter a number between 1 to 7: '));
89+
90+
switch (number) {
91+
case 1:
92+
console.log("Sunday");
93+
case 2:
94+
console.log("Monday");
95+
case 3:
96+
console.log("Tuesday");
97+
case 4:
98+
console.log("Wednesday");
99+
case 5:
100+
console.log("Thursday");
101+
case 6:
102+
console.log("Friday");
103+
case 7:
104+
console.log("Saturday");
105+
default:
106+
console.log("Invalid Number");
107+
}
108+
```
109+
**Output**
110+
```
111+
Wednesday
112+
Thursday
113+
Friday
114+
Saturday
115+
Invalid Number
116+
```
117+
***
118+
## JavaScript switch With Multiple Case
119+
```js
120+
const day = prompt('Enter the day: ');
121+
122+
switch (day) {
123+
case 'Monday':
124+
case 'Tuesday':
125+
case 'Wednesday':
126+
case 'Thursday':;
127+
case 'Friday':
128+
console.log("Weekday");
129+
break;
130+
case 'Saturday':
131+
case 'Sunday':
132+
console.log("Weekend");
133+
break;
134+
default:
135+
console.log("Invalid Day");
136+
}
137+
```
138+
**Output**
139+
```
140+
Weekday
141+
```
142+
## Simple Calculator
143+
```js
144+
// take the operator input
145+
const operator = prompt('Enter operator ( either +, -, * or / ): ');
146+
147+
// take the operand input
148+
const number1 = parseFloat(prompt('Enter first number: '));
149+
const number2 = parseFloat(prompt('Enter second number: '));
150+
151+
let result;
152+
switch(operator) {
153+
case '+':
154+
result = number1 + number2;
155+
break;
156+
case '-':
157+
result = number1 - number2;
158+
break;
159+
case '*':
160+
result = number1 * number2;
161+
break;
162+
case '/':
163+
result = number1 / number2;
164+
break;
165+
166+
default:
167+
console.log('Invalid operator');
168+
break;
169+
}
170+
console.log(`${number1} ${operator} ${number2} = ${result}`);
171+
```
172+
**Output**
173+
```
174+
Enter operator: *
175+
Enter first number: 5
176+
Enter second number: 8.8
177+
5 * 8.8 = 44
178+
```
179+
***
180+
## Assignment and Task
181+
**Can you use switch statements to create a program that takes the number input from the user from 1 to 12. And, print the corresponding month based on the input value.**
182+
### Solution:
183+
```js
184+
const number = parseInt(prompt('Enter a number from 1 to 12: '));
185+
186+
let result;
187+
switch(number) {
188+
case 1:
189+
result = "January";
190+
break;
191+
case 2:
192+
result = "Febraury";
193+
break;
194+
case 3:
195+
result = "March";
196+
break;
197+
case 4:
198+
result = "April";
199+
break;
200+
case 5:
201+
result = "May";
202+
break;
203+
case 6:
204+
result = "June";
205+
break;
206+
case 7:
207+
result = "July";
208+
break;
209+
case 8:
210+
result = "August";
211+
break;
212+
case 9:
213+
result = "September";
214+
break;
215+
case 10:
216+
result = "October";
217+
break;
218+
case 11:
219+
result = "November";
220+
break;
221+
case 12:
222+
result = "December";
223+
break;
224+
default:
225+
console.log('Invalid number');
226+
break;
227+
}
228+
console.log(result);
229+
```
230+
**Output**
231+
```
232+
Enter a number from 1 to 12: 3
233+
March
234+
```
235+
***
236+
## p4n Quiz
237+
**Q. Which of the cases is executed in the following code?**
238+
```js
239+
let value = 4;
240+
241+
switch(value) {
242+
case 1:
243+
case 2:
244+
default:
245+
}
246+
```
247+
1. case 1
248+
2. case 2
249+
3. case 4
250+
4. default
251+
252+
Answer: 4

0 commit comments

Comments
 (0)