Skip to content

javascript 2#1

Open
sachben91 wants to merge 2 commits intomasterfrom
sachin-benny
Open

javascript 2#1
sachben91 wants to merge 2 commits intomasterfrom
sachin-benny

Conversation

@sachben91
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@Mister-Corn Mister-Corn left a comment

Choose a reason for hiding this comment

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

Miscommented. Sorry about that!

Copy link
Copy Markdown

@Mister-Corn Mister-Corn left a comment

Choose a reason for hiding this comment

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

Objectives

Higher Order Functions and Callbacks

  • Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.

Array Methods

  • Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
    (Ron: Everything is completed, but you didn't use map for Challenge 2)
  • Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.

Closures

  • Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.

Comments

Good job, Sachin! You are definitely getting the hang of this. The only things I need from are from array methods: do Challenge 2 using map, and complete Challenge 5: Problem 3.

Additionally, try to commit more often. Having more commits better show your progress on the project, and helps you rewind back to a safe state if something goes wrong. Furthermore, you are expected to commit often to communicate to other devs what you are doing at work, so it's a good idea to develop this habit now.

TODO: Complete Challenge 5: Problem 3, and use map for Challenge 2.

// 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);
runners.forEach(function(names)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Gotta use map here, sorry! 😢

// 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 = [];

let ticketPriceTotal = runners.reduce((totaldonations, runner) => {return totaldonations += runner.donation;}, 0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's not necessary to do += here: it's good enough to say totalDonations + runner.donation. The reason being is that whatever the result of the callback function is, reduce will automatically make it to be the new value for totalDonations, which it will then pass in the next time it calls the callback function, or return if it's already done.

console.log(companyNames)

// Problem 3 No newline at end of file
// Problem 3
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 need one moooaaarrrrr!!!

// last passes the last item of the array into the callback.
return cb(arr[arr.length-1])
}
last(items, function(lastitem){console.log(lastitem)})
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 consistently pass a callback function which logs to the console whatever value is passed into it. While you change the name of the parameter on each one, it is essentially the same function.

Remember, you can use functions like they're values, and save them to variables. You can then use that variable to refer to that function:

function consoleLog(input) {
    console.log(input);
}

getLength(items, consoleLog);
last(items, consoleLog);
sumNums(x, y, consoleLog);

// Pass true to the callback if it is, otherwise pass false.
}

return (console.log(list.includes(item))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great play here with includes!

const ln = "Benny";
console.log(`${fn} ${ln}`);
}
fullname();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remember, you don't need to have a variable in the global scope to have a closure. Something like this would work too:

function fullName() {
    const fn = "Sachin";

    return function sayName() {
        const ln = "Benny";
        return `${fn} ${ln}`;
    }
}

const sayName = fullName();
console.log(sayName()); // Sachin Benny

@sachben91
Copy link
Copy Markdown
Owner Author

@Mister-Corn pushed changes to this assignment

Copy link
Copy Markdown

@Mister-Corn Mister-Corn left a comment

Choose a reason for hiding this comment

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

The finishing touches here are great. Thank you for getting this done!

allCaps = runners.map(function(names)
{
allCaps.push(names.first_name.toUpperCase())
return names.first_name.toUpperCase()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done! You're taking advantage of the fact that map creates a new array for you, and pushes elements into it based on the result of the callback. 💯

let newCompanyNames = []
newCompanyNames = runners.map(function(company_name){
return (`${company_name} inc`)
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Another good example of map. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants