Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added 3 exercises to js school #80

Merged
merged 10 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion pages/hack-school/js.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ToggleCodeBlock from './toggle_component';

# Week 2: JavaScript

## What is JavaScript?
Expand Down Expand Up @@ -136,6 +138,18 @@ const communites = ["Hack", "AI", "Cyber", "Design"];
const date = new Date("10-18-2023");
```

## Exercise 1: Javascript Syntax Quiz
Create a variable called firstName, that has your first name as it's value.
Then, create a variable called lastName, that has your last name as it's value.
Using the console.log() function, print the two variables together.
<ToggleCodeBlock>
```javascript copy showLineNumbers
let firstName = "Nishant"
let lastName = "Balaji"
console.log(firstName + " " + lastName) // Nishant Balaji
```
</ToggleCodeBlock>

## Operators
Operators in JavaScript perform actions on values and variables.
These include arithmetic, comparison, logical, and assignment operators.
Expand Down Expand Up @@ -238,7 +252,24 @@ console.log(organization[age]);; // prints 6
organization.hackschool(); // prints "JavaScript Workshop"
```

## Exercise 2: Creating and Manipulating Objects
Create an object representing a UCSD student. Include properties such as name, college, year, and favorite subject.
Then, write a function that updates the student's favorite subject.

<ToggleCodeBlock>
```javascript copy showLineNumbers
let student = {
name: "Billy",
college: "Sixth",
year: "Freshman",
favoriteSubject: "Math",
}

function updateFavoriteSubject(student, subject) {
student.favoriteSubject = subject
}
```
</ToggleCodeBlock>
## Loops
Loops allow you to execute a block of code repeatedly. Common loop types are `while`and `for` loops.

Expand Down Expand Up @@ -405,6 +436,28 @@ obj.greeting();
// prints window
```

## Exercise 3: Creating Functions
Create your own array of food items.
Then, write a function findFood() that takes in the array and a food item.
It will loop through the array and return true if the item exists in the array, and false otherwise.
<ToggleCodeBlock>
```javascript copy showLineNumbers
let foodList = ["banana", "apple", "peach", "orange"]

function findFood(foodList, food){
for (item in foodList){
if (item === food){
return true
}
}
return false
}

console.log(findFood(foodList, "apple")) // true
console.log(findFood(foodList, "kiwi")) // false
```
</ToggleCodeBlock>

## Callbacks
Callbacks are functions passed as arguments to other functions. They are commonly used for asynchronous programming and event handling.

Expand Down Expand Up @@ -494,4 +547,35 @@ findUser()
console.log("Age:", age + 5);
})
.catch((error) => console.error("Error:", error.message));
```
```

## Exercise 4: Async await practice

Create an async function fetchData that simulates an API call to fetch user data. The function should return a promise that resolves after 2 seconds with a user object containing name and email.

Then, write another async function displayUser that uses await to call fetchData and logs the user's information to the console.

<ToggleCodeBlock>
```javascript copy showLineNumbers
// Simulate API call using a Promise that resolves after 2 seconds
async function fetchData() {
return new Promise((resolve) => {
setTimeout(() => {
resolve({ name: "John Doe", email: "[email protected]" });
}, 2000);
});
}

// Use async/await to fetch and display user data
async function displayUser() {
try {
const user = await fetchData();
console.log("User Name:", user.name);
console.log("User Email:", user.email);
} catch (error) {
console.error("Error:", error.message);
}
}
// Call the displayUser function displayUser();
```
</ToggleCodeBlock>
25 changes: 25 additions & 0 deletions pages/hack-school/toggle_component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useState, FC, ReactNode } from 'react';

const ToggleCodeBlock: FC<{ children: ReactNode }> = ({ children }) => {
const [isVisible, setIsVisible] = useState(false);

return (
<div>
<button
type="button"
onClick={() => setIsVisible(!isVisible)}
angela139 marked this conversation as resolved.
Show resolved Hide resolved
style={{
backgroundColor: '#494D5F',
borderRadius: '4px',
padding: '0.5rem',
margin: '0.5rem',
}}
>
{isVisible ? 'Hide Code' : 'Show Code'}
</button>
{isVisible && <code>{children}</code>}
</div>
);
};

export default ToggleCodeBlock;
Loading