Home

In here I have the code of some of my Codecademy lessons.

Some routines I used...

I needed to create a 2D array of each word in 'overusedWords' with a count of 0
eg...
['really'][0]
['very'][0]
etc...
So I did the following...

var overusedWords = ['really', 'very', 'basically'];

var usedWords = [];

overusedWords.forEach(ow => {
   usedWords.push(ow, 0);
});


End Result: usedWords = [ [ 'really', 0 ], [ 'very', 0 ], [ 'basically', 0 ] ]

so
usedWords[1][0] = "very"
usedWords[1][1] = 0






Some routines I used...part 2

I needed to create an array of pizza options and then create a message saying No to each of the options.
The main point was ...
Write a function declineEverything() that takes in an array of strings and, using .forEach(), loops through each element in the array and calls politelyDecline() with each of them.
The .forEach() function should apply politelyDecline() directly; it should NOT merely receive an argument function that uses politelyDecline().


So I did the following...

const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];

var outputMsg = "";

const politelyDecline = (veg) => {
   outputMsg += 'No ' + veg + ' please. I will have pizza with extra cheese.
'; }

const declineEverything = function(arr) {
   // This is the bit that caused problems...
   // as 'politelyDecline' is a function that accepts 1 parameter...
   // but I don't pass one in as the '.forEach' handles that automatically.

   arr.forEach(politelyDecline);
}

declineEverything(veggies);
document.getElementById("test2").innerHTML = outputMsg;






Top

Some routines I used...part 3

I had to create an array that holds properties
then sort by the number of teeth (.numTeeth) property.

So I did this...

const speciesArray = [
   {speciesName:'shark', numTeeth:50},
   {speciesName:'dog', numTeeth:42},
   {speciesName:'alligator', numTeeth:80},
   {speciesName:'human', numTeeth:32}
];

//SORT by numTeeth
function sortSpeciesByTeeth(arr) {
   var newArr = arr.sort(function(a, b){return a.numTeeth - b.numTeeth});
   return newArr;
};

//Display array, numTeeth
function showSpeciesByTeeth(arr) {
   arr.forEach(function(a) {
      console.log(a, a.numTeeth);
   });
};

//Run the sort function
console.log(sortSpeciesByTeeth(speciesArray))

// Should print (in order of numTeeth):
// [ { speciesName: 'human', numTeeth: 32 },
// { speciesName: 'dog', numTeeth: 42 },
// { speciesName: 'shark', numTeeth: 50 },
// { speciesName: 'alligator', numTeeth: 80 } ]

//Will show array, numTeeth
showSpeciesByTeeth(speciesArray);

Some routines I used...part 4

I had to create a 'factory function' that returns an object of properties and methods
that uses Getters/Setters as well.
I was told what it had to achieve so I did this...

function dogFactory(name, breed, weight) {
   var dog = {
      _name: name,
      _breed: breed,
      _weight: weight,

      get name() {return this._name},
      get breed() {return this._breed},
      get weight() {return this._weight},

      set name(name) {this._name = name},
      set breed(breed) {this._breed = breed},
      set weight(weight) {this._weight = weight},

      bark() {return "ruff! ruff!"},
      eatTooManyTreats() {this._weight++}

   };

   return dog;
};

//Creates object 'tilly'
var tilly = dogFactory("Tilly", "Schnauzer", 13);

//The below uses the methods in 'tilly' to displays to the console the info
console.log(tilly);
console.log(tilly.bark());
console.log(tilly.weight);

//Runs the method and adds 1 to the weight then redisplays the weight
tilly.eatTooManyTreats();
console.log(tilly.weight);


Top

Some routines I used...part 5

I had to create a little game called Find My Hat
It runs from the Command Prompt, using NODE
and the code can be found in
D:/VisualStudioWeb/codecademyDemos/
    FindYourHat/FindYourHat.main.js


But a snippet from main.js looks like...

class Field {
    constructor(field) {
        this._field = field
    }

    get field() {
        return this._field;
    }

    print() {
        let display = "";
        this._field.forEach(function (fieldLine) {
            fieldLine.forEach(function (fieldChr) {
                display = display + fieldChr;
            });
            display = display + "\n";
        });
    console.log(display);
    }

}

function checkStatus(p1, p2) {
    if (myField.field[pos1][pos2] == hole) { return 2; }
    if (myField.field[pos1][pos2] == hat) { return 3; }
    return 0;
}

const myField = new Field([
    ['*', '░', '░', 'O', '░', '░'],
    ['░', 'O', '░', '░', '░', '░'],
    ['░', '^', '░', '░', '░', '░'],
]);


This is just to show a class that holds an array of arrays & how to retrieve the information.
So...
Within the class Field is a property (field)
The const myField = new Field([ ... creates a class called myField and sets the array to hold 3 arrays.
You then interrogate the class (myField) by using, for example...
myField.field[pos1][pos2]

....just an explanation of a small part of it.


Top