Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Try opening up your terminal and typing 'node'.
- // Then in the node REPL, copy and paste the commands in this docuemnt.
- // You can copy and paste the entire line of code including the comments into the REPL.
- // Intro: REPLS
- // A REPL is a 'Read Evaluate Print Loop'. It allows you to run commands in JS in your
- // terminal and then see the 'printed' result back in your screen.
- console.log('You can', 'chain multiple messages', 'inside console.log');
- // I. Basic data types
- // 1. Numbers
- 1 + 1; // add
- 1 - 1; // subtract
- 5 * 5; // multiply
- 10 / 2; // divide
- 6 % 3; // modulo: the remainder of division
- // 2. Strings
- 'hello world!';
- // Provide the number of chartacters in the string
- 'hello world!'.length;
- // Slice a piece of the string
- 'hello world!'.substring(1,5);
- // convert the string to upper case
- 'hello world!'.toUpperCase();
- // convert the string to lower case
- 'hello world!'.toLowerCase();
- // access a specific character in the string
- 'hello world!'[0];
- //2.1 String interpolation
- const message = 'world';
- const say = `Hello, ${message}!`; //you must use the ` sign. The ' sign will not allow interpolation.
- console.log(say);
- // 3. Boolean
- true;
- false;
- // 4. Null and undefined
- // Both these values can be used interchangeably. They describe a variable that has no content.
- null;
- undefined;
- // 4. Checking the data type
- typeof 1;
- typeof 'hello';
- typeof true;
- typeof false;
- // 5. Type conversion
- // 5.1 convert a string '5' into a number 5.
- parseInt('5', 10);
- // 5.2 convert an integer 5 into a string '5'.
- String(5);
- // II. Variables
- // 1. let
- // We use let when we want to re-assign a variable. Re-assignment is wehn we use '='.
- let fruit = 'banana';
- fruit = [1,2,3];
- // 2. const
- // We use const when we do not want to re-assign a variable. It has a unchanging value.
- const tropicalFruit = 'pineapple';
- tropicalFruit = 'mango'; // This line of code cannot be executed!
- // Note: In JS we use camelCase when naming variables. In ruby we used snake_case.
- // camelcase
- anotherVariable = undefined;
- // snake_case
- another_variable = undefined;
- // III. Arrays
- // 1.1 CRUD on arrays
- // Create an array
- const numbers = [1,2,3,4,5];
- // Read from an array
- numbers[0];
- // Update an array
- numbers.push(99); // Add 99 to the array
- numbers[0] = 99; // reassigned value at index 0 to 99
- numbers.splice(0,0,99); // at index 0, delete 0 items, and insert a number 99.
- // Delete from an array
- numbers.splice(0,1); // at index 0, delete 1 item.
- numbers.splice(0,2); // at index 0, delete 2 items.
- // 1.2 Other array operations
- // Split a string into an array of characters
- 'hello world'.split('');
- // Give an array length
- numbers.length;
- // The spread operator
- const male = ['hamlet', 'falstaff'];
- const female = ['gertrude', 'ophelia'];
- const people = [...male, ...female];
- console.log(people);
- // 1.3 Looping through an array
- numbers.forEach( (number, index) => {
- console.log(`${number} is at index: ${index}`);
- });
- // III. Control Flow
- // 1. If-Else statements
- const age = 12;
- if (age > 18) {
- console.log('can vote');
- } else if (age > 99) {
- console.log('can vote, and also very old.')l;
- } else {
- console.log('Too young to vote');
- }
- // 2. Ternary operator
- // This operation has two stages:
- // stage 1: check if the condition is true (is the variable 'age' > 18?)
- // stage 2: depending on the result, assign the value to 'result').
- // If the condition passes, result is set to the value to the left ('can vote')
- // If the condition does not passe, result is set to the value to the right ('cannot vote')
- const result = (age > 18) ? 'can vote' : 'cannot vote';
- console.log(result);
- // 2.2 Falsy and Truthy values
- // JS allows some values to function as if they were boolean 'false'.
- 1 == false
- // => true
- '' == false
- // => true
- // IV. Objects
- // 1. CRUD on objects
- // Create an object
- const characters = { hamlet: 'hello.', harry: 'hi.'};
- // read from an object
- chracters.hamlet
- characters['hamlet'] // the dot notation is preferred
- // update an object
- chracters.hamlet = 'hello world';
- // delete from an object
- delete characters.hamlet
- // 1.1 Looping through an object
- Object.keys(characters).forEach( key => {
- console.log(key);
- });
- Object.values(characters).forEach( value => {
- console.log(value);
- });
- // V. Maps
- // A map is like a JS Object, except you can set any data type as the key.
- // In a JS object, the kety MUST be a string.
- // 1. CRUD on maps
- // Create a map
- const myMap = new Map();
- myMap.set('hello', 'world'); // create a new key value pair in the map such that ['hello', 'world']
- // read from a map
- myMap.get('hello');
- // update a map
- myMap.set('hello', 'some new value');
- // delete from an object
- myMap.delete('hamlet');
- // 1.1 Looping through a map
- // A map is treated like an array: the first parameter is the 'value', the second parameter is the 'index'.
- myMap.forEach( (value, key) => {
- console.log(value, key);
- });
- // VI. Functions
- // 1. Arrow Functions
- // I define the function here
- const sayHello = () => {
- console.log('hello!'); // this is the 'block' passed into the function
- };
- sayHello(); // I execute the function here
- // 1.1 Single Line Functions
- // If your block (the { ... } of the function) contains 1 line of code, you can write your
- // function like this:
- const sayHi = () => console.log('hi!');
- sayHi();
- // 2. Building a Capitalise function
- // JS does not include a .capitalize function in its standards library. Let's make one.
- const capitalize = (string) => {
- // capitalize the firct character of the word and save it to a variable
- const firstLetter = string[0].toUpperCase();
- // make sure the rest of the string excluding the first character is lower case, and save it to a variable
- const restOfWord = string.substring(1, string.length).toLowerCase();
- // return the capitalized first letter + the rest of the word in lower case
- return firstLetter + restOfWord;
- };
- console.log(capitalize('all lower case.'));
- // 3. Going further: Let's build a function that will capitalize every word in a sentence.
- // Try saving this code in a file and run the following command in your terminal:
- // node --inspect-brk file_name.js
- // This will allow you to pause the script during execution so you can see what is happenning
- // in it line by line.
- const capitalizeSentence = (string) => {
- // Define an array that will collect our capitalised words.
- let arr = [];
- // Split the string into an array of words
- string.split(' ').forEach( word => {
- // capitalize the firct character of the word and save it to a variable
- let firstLetter = word[0].toUpperCase();
- // make sure the rest of the string excluding the first character is lower case, and save it to a variable
- let restOfTheWord = word.substring(1).toLowerCase();
- // create a new variable that combines the two new variables we just created
- let newWord = firstLetter + restOfTheWord;
- // push this new variable to the array we defined earlier
- arr.push(newWord);
- });
- // convert the array of capitalized words into a string again
- return arr.join(' ');
- }
- console.log(capitalizeSentence('all lower case.'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement