js programing question
js programing question
---
## **JavaScript Basics**
Let’s dive into the foundational concepts of JavaScript with clear explanations and
examples to get you started.
---
---
#### Example:
```javascript
let name = "John"; // Variable that can be changed
const age = 30; // Constant, cannot be changed
var isStudent = true; // Old way, works but not recommended
---
#### Example:
```javascript
let message = "Hello, World!"; // String
let score = 99; // Number
let isActive = false; // Boolean
let nothingHere = null; // Null
let notAssigned; // Undefined
let user = { name: "Alice", age: 25 }; // Object
let colors = ["red", "green", "blue"]; // Array
---
#### Example:
```javascript
// Arithmetic
let sum = 5 + 3; // Addition
let product = 5 * 3; // Multiplication
// Comparison
console.log(10 > 5); // true
console.log(10 === "10"); // false (strict equality)
// Logical
console.log(true && false); // false
console.log(true || false); // true
// Assignment
let x = 10;
x += 5; // Same as x = x + 5
console.log(x); // 15
```
---
#### Example:
```javascript
let age = 18;
---
### **6. Loops**
Loops allow you to repeat tasks.
#### Example:
```javascript
// For Loop
for (let i = 0; i < 5; i++) {
console.log("Count:", i);
}
// While Loop
let count = 0;
while (count < 3) {
console.log("While Count:", count);
count++;
}
```
---
#### Example:
```javascript
function greet(name) {
return `Hello, ${name}!`;
}
---
#### Example:
```html
<!DOCTYPE html>
<html>
<body>
<h1 id="title">Old Title</h1>
<button onclick="changeTitle()">Change Title</button>
<script>
function changeTitle() {
document.getElementById("title").textContent = "New Title!";
}
</script>
</body>
</html>
```
---
## **Next Steps**
1. Practice creating small scripts to solidify the basics.
2. Build simple interactive webpages (e.g., a counter or to-do list).
3. Explore modern JavaScript features such as:
- **Arrow Functions**
- **Template Literals**
- **Modules**
---
## **Practice Challenges**
Here’s a list of programming exercises, categorized by difficulty, to reinforce
your JavaScript skills:
### **Beginner**
1. **Basic Syntax and Variables**:
- Swap two numbers without using a third variable.
- Calculate the area of a rectangle given its dimensions.
2. **Control Flow**:
- Check if a number is even or odd.
- Find the largest of three numbers.
3. **Loops**:
- Print numbers from 1 to 100. Replace multiples of 3 with "Fizz", multiples of
5 with "Buzz", and both with "FizzBuzz".
- Generate the Fibonacci sequence up to `n` terms.
4. **Strings**:
- Reverse a string without built-in methods.
- Count vowels in a string.
5. **Arrays**:
- Find the maximum and minimum values in an array.
- Remove duplicates from an array.
### **Intermediate**
1. **Functions**:
- Write a function to check if a string is a palindrome.
- Create a function to calculate the factorial of a number using recursion.
2. **Objects**:
- Create an object representing a student and add a method to calculate their
average grade.
- Merge two objects, handling conflicting keys.
3. **Higher-Order Functions**:
- Use `map()` to square all numbers in an array.
- Use `filter()` to find numbers greater than 50 in an array.
4. **DOM Manipulation**:
- Build a counter app with buttons for increment, decrement, and reset.
- Create a to-do list with add, mark complete, and delete functionalities.
5. **Asynchronous Programming**:
- Fetch data from an API (e.g.,
[JSONPlaceholder](https://jsonplaceholder.typicode.com/)) and log the response.
- Implement a countdown timer using `setTimeout`.
### **Advanced**
1. **Closures**:
- Create a function that tracks how many times it has been called.
- Write a curried function that adds numbers one at a time.
2. **Promises & Async/Await**:
- Fetch data from multiple APIs simultaneously using `Promise.all`.
- Simulate an API call with a delay using `setTimeout` and `Promise`.
3. **Data Structures**:
- Implement a stack or queue using arrays.
- Sort an array of objects by a specific property.
4. **Advanced DOM**:
- Build a dynamic table from a JSON array.
- Create a fully functional calculator UI.
---