Skip to content

Commit

Permalink
Small improvements to JavaScript & TypeScript codeblocks (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieweinberger authored Oct 3, 2024
1 parent 94699f5 commit 839ac6f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
16 changes: 8 additions & 8 deletions docs/unit6.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Here's a quick overview of the basics.
```javascript
console.log("Hello, World!");
// In Web Development, this will actually show up in the developer console.
// Open the console in Chrome with cmd+alt+j (mac) or ctrl+shift+j (windows)
// Open the console in Chrome with Cmd+Alt+J (mac) or Ctrl+Shift+J (windows)
```

### Variables
Expand Down Expand Up @@ -99,7 +99,7 @@ console.log(array); // "['a', 'b', 'c', 1, 2, 3, '4']"
const str = array.join('-'); // Combine elements into a string
console.log(str); // "a-b-c-1-2-3-4"

//get index of an element (first occurrence)
// get index of an element (first occurrence)
console.log(array.indexOf('x')); // 3

array.reverse(); // Reverse the array
Expand Down Expand Up @@ -154,7 +154,7 @@ const numbers = [1, 2, 3, 4];
const doubled = numbers.map(function(item) {
return item * 2;
});
console.log(doubled); // "[2, 4, 6, 8]"
console.log(doubled); // [2, 4, 6, 8]
```

#### Filter
Expand All @@ -165,7 +165,7 @@ const numbers = [1, 2, 3, 4];
const evens = numbers.filter(function(item) {
return item % 2 === 0;
});
console.log(evens); // "[2, 4]"
console.log(evens); // [2, 4]
```

#### Reduce
Expand All @@ -182,10 +182,10 @@ const numbers = [1, 2, 3, 4];
// Callback Function parameters
// result - the accumulator, which is the returned value of the previous iteration.
// item - the current item in the array.
//

// Note: the intial value of the accumulator is set to 0.
const sum = numbers.reduce(function (result, item) { return result + item; }, 0);
console.log(sum); // "10"
console.log(sum); // 10
```

### Conditionals
Expand Down Expand Up @@ -295,7 +295,7 @@ Unlike python, you don't need quotes around the keys.
const ages = {alice:40, bob:35, charles:13}
const things = {num:12, dog:'woof', list:[1,2,3]}

//both of these work!
// both of these work!
console.log(ages["alice"]); // 40
console.log(ages.bob); // 35
```
Expand Down Expand Up @@ -400,7 +400,7 @@ Create a new file with the `.js` file extension.
Add this to the file:

```javascript
//hello.js
// hello.js
console.log("Hello, World!")
```

Expand Down
43 changes: 24 additions & 19 deletions docs/unit7.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ React is a JavaScript library to build user interfaces, usually websites. Create

Alright, one more small thing before we dive in to React! React uses JavaScript, but more specifically, it's called JSX. This is a syntax extension to JavaScript.

```jsx'
```jsx
// an example of JSX
const element = <h1>Hello, world!</h1>;
```
Expand All @@ -57,9 +57,11 @@ JSX combines our markup language (HTML-like), and our logic (JavaScript). This l
```jsx
const name = 'Raman Gupta';
const element = <h1>Hello, {name}</h1>;
//we can do this in element attributes as well
const imgURL = "./path/to/image.png"

// we can do this in element attributes as well
const imgURL = "./path/to/image.png";
const imgElement = <img src={imgURL} />;

// we use className instead of class to give elements a class
const withClass= <h2 className="example" />;
```
Expand Down Expand Up @@ -103,13 +105,14 @@ This component returns what we want our user to see on their screen. More specif

```jsx
React.createElement("div", {
className: "shopping-list"
}, React.createElement("h1", null, "Shopping List for ", props.name),
React.createElement("ul", null,
React.createElement("li", null, "Instagram"),
React.createElement("li", null, "WhatsApp"),
React.createElement("li", null, "Oculus")
)
className: "shopping-list"
},
React.createElement("h1", null, "Shopping List for ", props.name),
React.createElement("ul", null,
React.createElement("li", null, "Instagram"),
React.createElement("li", null, "WhatsApp"),
React.createElement("li", null, "Oculus")
)
);
```

Expand Down Expand Up @@ -178,12 +181,14 @@ In this instance of the `Welcome` component, it can access this data using the `

```jsx
<Welcome name="Sara" title="Ms." />
//In the component the props object would look like this:

// In the component the props object would look like this:
props = {
"name": "Sara",
"title": "Ms."
}
//We can use props like this

// We can use props like this
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Expand Down Expand Up @@ -255,8 +260,8 @@ This is something called Array Destructuring, a cool feature of JavaScript.
```javascript
let [greeting, pronoun] = ["Hello", "I" , "love", "AntAlmanac"];

console.log(greeting);//"Hello"
console.log(pronoun);//"I"
console.log(greeting); // "Hello"
console.log(pronoun); // "I"
```

In our example in React, `useState` returns an array of 2 values. The first being the value of the state variable. The second is a function to set the state variable.
Expand All @@ -270,18 +275,18 @@ We can also do Object Destructuring. This is similar to arrays, but we are inste
```javascript
let {name, organization, role} = {name: "Raman", organization: "ICSSC", role: "Projects Co-Chair"};

console.log(name);//"Raman"
console.log(organization);//"ICSSC"
console.log(role);//Projects Co-Chair"
console.log(name); // "Raman"
console.log(organization); // "ICSSC"
console.log(role); // "Projects Co-Chair"
```

Note the names on the left hand side should have the same name as the property key in the object.
If we want to assign a new variable name, we can. We also don't need to extract the entire object.
```javascript
let {name, organization: club} = {name: "Raman", organization: "ICSSC", role: "Projects Co-Chair"};

console.log(name);//"Raman"
console.log(club);//"ICSSC"
console.log(name); // "Raman"
console.log(club); // "ICSSC"
```

This is also often used in function arguments.
Expand Down
16 changes: 9 additions & 7 deletions docs/unit9.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const user: User = {
}
// We will get this error:
// Type '{ username: string; id: number; }' is not assignable to type 'User'.
// Object literal may only specify known properties, and 'username' does not exist in type 'User'.ts(2322)
// Object literal may only specify known properties, and 'username' does not exist in type 'User'.ts(2322)
```

We can make our object more flexible by making it indexable.
Expand All @@ -125,7 +125,8 @@ interface User {
id: number;
[key: string]: any;
}
//This is now allowed

// This is now allowed
const user: User = {
name: "Raman",
username: "ramanxg",
Expand All @@ -145,7 +146,8 @@ We can use string or number literals to define a set of values a value can be, o
```tsx
type LockStates = "locked" | "unlocked";
type someString = string | string[];
//both are valid

// both are valid
let a: someString = "hello"
let b: someString = ["hello", "world", "!"]
```
Expand Down Expand Up @@ -177,16 +179,16 @@ const y: MyType<number> = {
TypeScript is “transpiled” to JavaScript.

```tsx
//hello.ts
var courseNumber: number = 133;
// hello.ts
let courseNumber: number = 133;
console.log('Hello, IN4MATX ' + courseNumber + '!');
```

We transpile with the typescript compiler using`tsc hello.ts`.

```tsx
//hello.js
var courseNumber = 133;
// hello.js
let courseNumber = 133;
console.log('Hello, IN4MATX ' + courseNumber + '!');
```

Expand Down

0 comments on commit 839ac6f

Please sign in to comment.