Skip to content

Instantly share code, notes, and snippets.

View sandrabosk's full-sized avatar
👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall

Aleksandra Bošković sandrabosk

👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall
  • Ironhack
View GitHub Profile

Callbacks

CALLBACKS are a way to make sure certain code doesn’t execute until another code has already finished execution.

Let's check this example:

  • function 1:
function study(what){
 console.log(`I am studying ${what}.`)
// https://gist.github.com/sandrabosk/d26d441d8e9a5912c6084b0f9e5c2c5d
// Goal: display the symbol "!" from every variable
const variable1 = [4, 'a', true, '!', 3.14];
console.log(1, variable1[3]);
console.log(1, variable1.toString()[9]);
const variable2 = {
a: '?',
// updated: 01/24/2020
// https://github.com/ironhack-labs/lab-javascript-functions-and-arrays
// ***********************************************************************************************
// Iteration #1: Find the maximum
// ***********************************************************************************************
function maxOfTwoNumbers(num1, num2) {
if (num1 > num2) {
return num1;

.map()

  • doesn't mutate the original array
  • returns a new array of the same size as the original array

We will be working on the following array:

.reduce()

  • doesn't mutate the original array
  • returns a new variable that holds the 'sum' (which implies it is the type of number in case when we are reducing numerical values)

Source

We will be working on the array of prices:

.filter()

  • doesn't mutate the original array
  • returns a new array with only the elements that satisfy conditions (returns a new array of smaller size than original (subarray))

We will be working on the following array:

const numbers = [3, 7, 9, 10, 12];

.sort()

  • mutates the original array - to prevent it, make a copy (const sorted = origArr.slice().sort() or const sorted1 = [...origArr]) of the array and then sort the copy
  • makes a specific order of elements
  • returns array
  • sorts by a string value (even if it's an array of numbers)

There's plenty of ways to sort arrays in JavaScript (and using .sort() is probably one of the slowest ones, but it is important to understand it and how it works).

When and why we should use length-1?

Very often you will see that length - 1 is being used and you might ask yourself why is that. Here is an example that will demo when and why you should use it and what is the alternative way.

Let's take variable firstName and has the length 4.

const firstName = "maya";
const someString = "hey there hey what up what"
const howMany = (blah) => {
let arrOfWords = blah.split(" ");
const mappedObj = {};
for(let i=0; i<arrOfWords.length; i++){
if(mappedObj[arrOfWords[i]]){
mappedObj[arrOfWords[i]]++
} else {
mappedObj[arrOfWords[i]]=1

Cloning arrays - basics

Introduction

To recap, mutable data types are:

  • arrays
  • objects.

All the other data types (the primitives) are immutable:

  • string,