Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,46 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];

// ==== Challenge 1: Use .forEach() ====
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
let fullName = [];
runners.foreach(` ${ runners[i]["first_name"] } ${ runners[i]["last_name"] }`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're mixing up forEach arr.forEach(element => element and for loops for (let i=0; i < arr.length; i++). They do the same thing but you can only use one or the other.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should have looked something like runners.forEach( runner => { fullName.push(`${runner.first_name} ${runner.last_name}`); }) the forEach just goes through the runners array for us. Then inside you do your action. So now full-name would have our first and last names.


runners.foreach(fullName.push(runners[i].first_name + " " + runners[i].last_name ));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inside you made fullName.push(runners[i].first_name + " " + runners[i].last_name is correct if you were trying to do for loops(we weren't trying to do for loops for this assignment though), you just would have to had put it in a for loop like this for (let i = 0; i < runners.length;i++){ fullName.push(runners[i].first_name + " " + runners[i].last_name ) }


console.log(fullName);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
let allCaps = [];
console.log(allCaps);
allCaps = runners.map(function(items) {return items.first_name.toUpperCase();});
console.log(allCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
let largeShirts = [];
largeShirts = runners.filter((list) => {return list.shirt_size === "L" ;});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runners.filter((list) => {return list.shirt_size === "L" ;}); could also be written as runners.filter(list => list.shirt_size === "L" ); (you don't need the return statement if it's written on the same line because it's implied. Parentheses are also optional on an arrow function if you're only passing 1 param.

console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
let ticketPriceTotal = [];
ticketPriceTotal = runners.reduce((total, next) => {return total + next.donation ;});
console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above.

// Problem 1
// Problem 1 - MAKE A NICKNAME FOR EACH CONTESTEANT USING THIER FIRST NAM AND SHIRT SIZE.
let nickname =[];
nickname = runners.map(items.first_name.toUpperCase.concat(items.shirt_size));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Checkout the syntax on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map.
  • Map requires a callback. Should be something like runners.map(items => items.first_name.toUpperCase.concat(items.shirt_size)). (This won't run either because your missing you parentheses on toUpperCase, it should be toUpperCase(). It will work after that though)
  • I would also change items to item or runner since it represents a single runner. (This is a really cool way of using concat to do it, first person I've seen using it)

console.log(nickname);

// Problem 2
// Problem 2 - MAKE A LIST OF THE T DONATIONS IN ORDER OF MOST TO LEAST.
let best_givers = [];
best_givers = runners.map(function(items) => {`${items.donation}, ${items.first_name}, ${items.last_name}`});
// sort numbers function...

// Problem 3
// Problem 3 -
let contact_list = [];
contact_list = runners.map(items.email);
console.log(contact_list);
76 changes: 69 additions & 7 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Create a callback function and invoke the function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', ''];

/*
/*

//Given this problem:

//Given this problem:

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
}
Expand All @@ -24,24 +24,86 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
let outPut = cb(arr);
return outPut;
}
function callback( arr ){
console.log(arr.length);
return arr.length;
}

getLength(items, callback);



function last(arr, cb) {
// last passes the last item of the array into the callback.
let output = cb(arr[arr.length -1]);
return output;
}

function sumNums(x, y, cb) {
function callback0(arr) {
console.log();
return console.log()
}

last( items , callback0);

// function callback( arr ){
// console.log(arr.length);
// return arr.length;
// }
// function callback1(){
// console.log(
// return output;
// }

last(items,callback0);


function sumNums(x, y) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
let outPut = (x + y);
return outPut;
}

function multiplyNums(x, y, cb) {
function publishSum(x, y, sumNums) {
let pub = console.log(sumNums(x , y));
return pub;
}

publishSum(10,20, sumNums);


function multiplyNums(x, y) {
// multiplyNums multiplies two numbers and passes the result to the callback.
let output = ( x * y );
return output;
}

function publishMul(multiplyNums){
return console.log(multiplyNums);
}

function contains(item, list, cb) {
publishMul(multiplyNums(4,3));




function contains(item, list) {
let tOrF = [];
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
for (let i = 0 ; i < list.length; i++){
if (list[i] === item) {
console.log(true);
return tOrF.push(true);
}
}
}
// return (Callback => {return console.log(tOrF)});
}
contains(items[0],items);

/* STRETCH PROBLEM */

Expand Down
31 changes: 31 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
function yell(name){
const n = name;
console.log(`Can you hear Me ${n}!!!!!!`);

function speak(nickName){
const nn = "pebbles";
console.log(` Hey, ${ nn } is your given name ${ n } , i'd never heard it before.`);

function whisper() {
const lower = "I'm sorry for speaking so loud." ;
console.log (`{lower} Hey,Would you rather me call you ${ nn } or ${ n }, i knowsome people havea preference.`)
}
whisper();
}
speak();
}
yell();




// ==== Challenge 2: Create a counter function ====
const counter = () => {
let count = 0;
return () => (++count);
// Return a function that when invoked increments and returns a counter variable.
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
const newCounter = counter();
console.log(newCounter())
console.log(newCounter())
console.log(newCounter())
console.log(newCounter())
console.log(newCounter())
console.log(newCounter())




/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */

Expand Down