Skip to content

Instantly share code, notes, and snippets.

@Oletem
Oletem / 0_reuse_code.js
Created September 27, 2017 20:54
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@Oletem
Oletem / Accessing_Nested_Arrays.js
Last active September 27, 2017 21:03
As we have seen in earlier examples, objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays.
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
@Oletem
Oletem / loop.js
Last active September 27, 2017 21:31
You can run the same code multiple times by using a loop. The most common type of JavaScript loop is called a "for loop" because it runs "for" a specific number of times. For loops are declared with three optional expressions separated by semicolons: for ([initialization]; [condition]; [final-expression]) The initialization statement is executed…
var myArray = [];
for(var i = 1; i<6; i++){ // Start counting from 1, in order not to get 0, 1, 2, 3
myArray.push(i);
};
myArray = [1,2,3,4,5];
@Oletem
Oletem / Odd_Even_Loop.js
Created September 27, 2017 21:39
For loops don't have to iterate one at a time. By changing our final-expression, we can count by even numbers. We'll start at i = 0 and loop while i < 10. We'll increment i by 2 each loop with i += 2. var ourArray = []; for (var i = 0; i < 10; i += 2) { ourArray.push(i); } ourArray will now contain [0,2,4,6,8]. Let's change our initialization so…
// Example
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
/*ourArray will now contain [0,2,4,6,8]. - Почему не десяти, потому, что меньше десяти, а ближайшее число меньше десяти на двойку, на которую мы увеличиваем - это 8*/
@Oletem
Oletem / For_loop_count_backwards.js
Created September 28, 2017 20:48
A for loop can also count backwards, so long as we can define the right conditions. In order to count backwards by twos, we'll need to change our initialization, condition, and final-expression. We'll start at i = 10 and loop while i > 0. We'll decrement i by 2 each loop with i -= 2. var ourArray = []; for (var i=10; i > 0; i-=2) { ourArray.push…
// Example
var ourArray = [];
for (var i = 10; i > 0; i -= 2) {
ourArray.push(i);
}
//[10,8,6,4,2]
@Oletem
Oletem / Profile_Lookup.js
Last active October 31, 2017 23:29
We have an array of objects representing different people in our contacts lists. A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you. The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact. If both are true, then…
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
@Oletem
Oletem / Gear.js
Created September 30, 2017 18:04
Objects have their own attributes, called properties, and their own functions, called methods. In the previous challenges, we used the this keyword to reference public properties of the current object. We can also create private properties and private methods, which aren't accessible from outside the object. To do this, we create the variable in…
var Car = function() {
// this is a private variable
var speed = 10;
// these are public methods
this.accelerate = function(change) {
speed += change;
};
@Oletem
Oletem / concat.js
Created October 1, 2017 12:35
concat can be used to merge the contents of two arrays into one. concat takes an array as an argument and returns a new array with the elements of this array concatenated onto the end. Here is an example of concat being used to concatenate otherArray onto the end of oldArray: newArray = oldArray.concat(otherArray); Use .concat() to concatenate c…
var oldArray = [1,2,3];
var newArray = [];
var concatMe = [4,5,6];
// Only change code below this line.
newArray = oldArray.concat(concatMe);
@Oletem
Oletem / join.js
Created October 1, 2017 12:37
We can use the join method to join each element of an array into a string separated by whatever delimiter you provide as an argument. The following is an example of using join to join all of the elements of an array into a string with all the elements separated by word and: var veggies = ["Celery", "Radish", "Carrot", "Potato"]; var salad = vegg…
var joinMe = ["Split","me","into","an","array"];
var joinedString = '';
// Only change code below this line.
joinedString = joinMe.join(" ");
@Oletem
Oletem / head_cut.js
Created October 8, 2017 17:35
Return the remaining elements of an array after chopping off n elements from the head. The head means the beginning of the array, or the zeroth index. Remember to use Read-Search-Ask if you get stuck. Write your own code. Here are some helpful links: Array.prototype.slice() Array.prototype.splice()
function slasher(arr, howMany) {
return arr.slice(howMany);
}
slasher([1, 2, 3], 2);