Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 23, 2018 15:55
Show Gist options
  • Save Anna-Myzukina/fd1c4df50bc4d1f7e98e5728cfd6a005 to your computer and use it in GitHub Desktop.
Save Anna-Myzukina/fd1c4df50bc4d1f7e98e5728cfd6a005 to your computer and use it in GitHub Desktop.
javascripting
//Exercise 1
/*To keep things organized, let's create a folder for this workshop.
Run this command to make a directory called javascripting (or something
else if you like):
mkdir javascripting
Change directory into the javascripting folder:
cd javascripting
Create a file named introduction.js:
touch introduction.js
Or if you're on Windows:
type NUL > introduction.js
(type is part of the command!)
Open the file in your favorite editor, and add this text:
console.log('hello');
*/
console.log('hello');
//Exercise 10
/*Create a file named for-loop.js.
In that file define a variable named total and make it equal the number 0.
Define a second variable named limit and make it equal the number 10.
Create a for loop with a variable i starting at 0 and increasing by 1 each
time through the loop. The loop should run as long as i is less than
limit.
On each iteration of the loop, add the number i to the total variable. To
do this, you can use this statement:
total += i;
After the for loop, use console.log() to print the total variable to the
terminal.
*/
var total = 0;
var limit = 10;
for (i = 0; i < limit; i++) {
total += i;
}
console.log( total);
//Exercise 11
/*Create a file named arrays.js.
In that file define a variable named pizzaToppings that references an
array that contains three strings in this order: tomato sauce, cheese,
pepperoni.
Use console.log() to print the pizzaToppings array to the terminal.*/
var pizzaToppings = ['tomato sauce', 'cheese', 'pepperoni'];
console.log(pizzaToppings);
//Exercise 12
/* There are many ways to manipulate arrays.
One common task is filtering arrays to only contain certain values.
For this we can use the .filter() method.
Here is an example:
var pets = ['cat', 'dog', 'elephant'];
var filtered = pets.filter(function (pet) {
return (pet !== 'elephant');
});
The filtered variable will now only contain cat and dog.
## The challenge:
Create a file named array-filtering.js.
In that file, define a variable named numbers that references this array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Like above, define a variable named filtered that references the result of
numbers.filter().
The function that you pass to the .filter() method will look something
like this:
function evenNumbers (number) {
return number % 2 === 0;
}
Use console.log() to print the filtered array to the terminal.*/
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var filtered = numbers.filter(function (evenNumbers) {
return evenNumbers %2 === 0;
});
console.log(filtered);
//Exercise 13
/*Array elements can be accessed through index number.
Index number starts from zero to array's property length minus one.
Here is an example:
var pets = ['cat', 'dog', 'rat'];
console.log(pets[0]);
The above code will print the first element of pets array - string cat.
Array elements must be accessed through only using bracket notation.
Dot notation is invalid.
Valid notation:
console.log(pets[0]);
Invalid notation:
console.log(pets.1);
## The challenge:
Create a file named accessing-array-values.js.
In that file, define array food :
var food = ['apple', 'pizza', 'pear'];
Use console.log() to print the second value of array to the terminal.*/
var food = ['apple', 'pizza', 'pear'];
console.log(food[1]);
//Exercise
/*For this challenge we will use a for loop to access and manipulate a list
of values in an array.
Accessing array values can be done using an integer.
Each item in an array is identified by a number, starting at 0.
So in this array hi is identified by the number 1:
var greetings = ['hello', 'hi', 'good morning'];
It can be accessed like this:
greetings[1];
So inside a for loop we would use the i variable inside the square
brackets instead of directly using an integer.
## The challenge:
Create a file named looping-through-arrays.js.
In that file, define a variable named pets that references this array:
['cat', 'dog', 'rat'];
Create a for loop that changes each string in the array so that they are
plural.
You will use a statement like this inside the for loop:
pets[i] = pets[i] + 's';
After the for loop, use console.log() to print the pets array to the
terminal.*/
var pets = ['cat', 'dog', 'rat'];
for(var i = 0; i < pets.length; i++) {
pets[i] = pets[i] + 's';
}
console.log(pets);
//Exercise 15
/*Objects are lists of values similar to arrays, except values are
identified by keys instead of integers.
Here is an example:
var foodPreferences = {
pizza: 'yum',
salad: 'gross'
};
## The challenge:
Create a file named objects.js.
In that file, define a variable named pizza like this:
var pizza = {
toppings: ['cheese', 'sauce', 'pepperoni'],
crust: 'deep dish',
serves: 2
};
Use console.log() to print the pizza object to the terminal.*/
var pizza = {
toppings: ['cheese', 'sauce', 'pepperoni'],
crust: 'deep dish',
serves: 2
};
console.log(pizza);
//Exercise 16
/*
You can access and manipulate object properties –– the keys and values
that an object contains –– using a method very similar to arrays.
Here's an example using square brackets:
var example = {
pizza: 'yummy'
};
console.log(example['pizza']);
The above code will print the string 'yummy' to the terminal.
Alternately, you can use dot notation to get identical results:
example.pizza;
example['pizza'];
The two lines of code above will both return yummy.
## The challenge:
Create a file named object-properties.js.
In that file, define a variable named food like this:
var food = {
types: 'only pizza'
};
Use console.log() to print the types property of the food object to the
terminal.*/
var food = {
types: 'only pizza'
};
console.log(food.types);
//Exercisw 17
/*A function is a block of code that takes input, processes that input, and
then produces output.
Here is an example:
function example (x) {
return x * 2;
}
We can call that function like this to get the number 10:
example(5)
The above example assumes that the example function will take a number as
an argument –– as input –– and will return that number multiplied by 2.
## The challenge:
Create a file named functions.js.
In that file, define a function named eat that takes an argument named
food that is expected to be a string.
Inside the function return the food argument like this:
return food + ' tasted really good.';
Inside of the parentheses of console.log(), call the eat() function with
the string bananas as the argument.
*/
function eat(food) {
return food +' ' + 'tasted really good.';
}
console.log(eat('bananas'));
//Exercise 18
/*A function can be declared to receive any number of arguments. Arguments
can be from any type. An argument could be a string, a number, an array,
an object and even another function.
Here is an example:
function example (firstArg, secondArg) {
console.log(firstArg, secondArg);
}
We can call that function with two arguments like this:
example('hello', 'world');
The above example will print to the terminal hello world.
## The challenge:
Create a file named function-arguments.js.
In that file, define a function named math that takes three arguments.
It's important for you to understand that arguments names are only used to
reference them.
Name each argument as you like.
Within the math function, return the value obtained from multiplying the
second and third arguments and adding that result to the first argument.
After that, inside the parentheses of console.log(), call the math()
function with the number 53 as first argument, the number 61 as second and
the number 67 as third argument.*/
function math(first, second, third) {
return (first + (second * third));
}
console.log(math(53, 61, 67));
//Exercise 19
/*Scope is the set of variables, objects, and functions you have access to.
JavaScript has two scopes: global and local. A variable that is declared
outside a function definition is a global variable, and its value is
accessible and modifiable throughout your program. A variable that is
declared inside a function definition is local. It is created and
destroyed every time the function is executed, and it cannot be accessed
by any code outside the function.
Functions defined inside other functions, known as nested functions, have
access to their parent function's scope.
Pay attention to the comments in the code below:
var a = 4; // a is a global variable, it can be accessed by the functions below
function foo() {
var b = a * 3; // b cannot be accessed outside foo function, but can be accessed by functions
// defined inside foo
function bar(c) {
var b = 2; // another `b` variable is created inside bar function scope
// the changes to this new `b` variable don't affect the old `b` variable
console.log( a, b, c );
}
bar(b * 4);
}
foo(); // 4, 2, 48
IIFE, Immediately Invoked Function Expression, is a common pattern for
creating local scopes.
Example:
(function(){ // the function expression is surrounded by parenthesis
// variables defined here
// can't be accessed outside
})(); // the function is immediately invoked
## The challenge:
Create a file named scope.js.
In that file, copy the following code:
var a = 1, b = 2, c = 3;
(function firstFunction(){
var b = 5, c = 6;
(function secondFunction(){
var b = 8;
(function thirdFunction(){
var a = 7, c = 9;
(function fourthFunction(){
var a = 1, c = 8;
})();
})();
})();
})();
Use your knowledge of the variables' scope and place the following code
inside one of the functions in scope.js so the output is a: 1, b: 8, c: 6
console.log("a: "+a+", b: "+b+", c: "+c);
*/
var a = 1, b = 2, c = 3;
(function firstFunction(){
var b = 5, c = 6;
(function secondFunction(){
var b = 8;
(function thirdFunction(){
var a = 7, c = 9;
(function fourthFunction(){
var a = 1, c = 8;
})();
})();
console.log("a: "+a+", b: "+b+", c: "+c);
})();
})();
//Exercise 2
/*Create a file named variables.js.
In that file declare a variable named example.
Make the variable example equal to the value 'some string'.
Then use console.log() to print the example variable to the console.
Check to see if your program is correct by running this command:
javascripting verify variables.js
*/
var example = 'some string';
console.log(example);
//Exercise 3
/*For this challenge, create a file named strings.js.
In that file create a variable named someString like this:
var someString = 'this is a string';
Use console.log to print the variable someString to the terminal.
*/
var someString = 'this is a string';
console.log(someString);
//Exercise 4
/*Create a file named string-length.js.
In that file, create a variable named example.
Assign the string 'example string' to the variable example.
Use console.log to print the length of the string to the terminal
*/
var example = 'example string';
console.log(example.length);
//Exercise 5
/*Create a file named revising-strings.js.
Define a variable named pizza that references this string: 'pizza is
alright'
Use the .replace() method to change alright to wonderful.
Use console.log() to print the results of the .replace() method to the
terminal.*/
var pizza = 'pizza is alright';
pizza = pizza.replace('alright', 'wonderful');
console.log(pizza);
//Exercise 6
/*Create a file named numbers.js.
In that file define a variable named example that references the integer
123456789.
Use console.log() to print that number to the terminal.*/
var example = 123456789;
console.log(example);
//Exercise 7
/*Create a file named number-to-string.js.
In that file define a variable named n that references the number 128;
Call the .toString() method on the n variable.
Use console.log() to print the results of the .toString() method to the
terminal.*/
var n = 128;
n = n.toString();
console.log(n);
//Exercise 7
/*Create a file named rounding-numbers.js.
In that file define a variable named roundUp that references the float
1.5.
We will use the Math.round() method to round the number up. This method
rounds either up or down to the nearest integer.
An example of using Math.round():
Math.round(0.5);
Define a second variable named rounded that references the output of the
Math.round() method, passing in the roundUp variable as the argument.
Use console.log() to print that number to the terminal*/
var roundUp = 1.5;
var rounded = Math.round(roundUp);
console.log(rounded);
//Exercise 9
/*Create a file named number-to-string.js.
In that file define a variable named n that references the number 128;
Call the .toString() method on the n variable.
Use console.log() to print the results of the .toString() method to the
terminal.*/
var fruit = 'orange';
if (fruit.length > 5) {
console.log('The fruit name has more than five characters.');
} else {
console.log('The fruit name has five characters or less.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment