Welcome to the first edition of Sunday Studies! Every Sunday I'll be releasing a code challenge for you to solve for the week. The solutions will be posted on Saturdays so you can see how I worked through the problem.
Code Challenge
You need to merge two arrays into one array for future manipulation.
Starting Point
This is what you have to start with:
var fruits = [
{ name: "apple", quantity: 5 },
{ name: "grapes", quantity: 8 },
{ name: "pear", quantity: 2 },
{ name: "mango", quantity: 10 },
{ name: "watermelon", quantity: 2 }
]
var desserts = [
{ name: "cookie", quantity: 12 },
{ name: "cake", quantity: 1 },
{ name: "candy", quantity: 25 },
{ name: "cinnamon roll", quantity: 48 }
]
var sweetStuff = []
console.log(sweetStuff)
Final Result
When you log sweetStuff
in the console, you should get everything within the array as shown below:
[
{ name: "apple", quantity: 5 },
{ name: "grapes", quantity: 8 },
{ name: "pear", quantity: 2 },
{ name: "mango", quantity: 10 },
{ name: "watermelon", quantity: 2 },
{ name: "cookie", quantity: 12 },
{ name: "cake", quantity: 1 },
{ name: "candy", quantity: 25 },
{ name: "cinnamon roll", quantity: 48 }
]
Extra Credit
For those who are knowledgeable of JavaScript already, your challenge is to solve this problem in a single line of code.
Guidelines
- You can use any language you want to solve it. Just know that the solution will be devised and written via JavaScript.
- Try and devise the solution on your own for at least 15 minutes before checking the internet for ideas.
- If you do use other resources to help you come up with the solution, no copying and pasting! Make sure to type it all out so that you get the repetition to reinforce what you're doing.
CodePen
Here's the CodePen for you to fork / work with to solve the problem.
See the Pen Sunday Studies #1: Problem by @bencodezen on CodePen.
Good luck!