Summary
A recap of how JavaScript differs from Java, and where to go further.
Apprentice · JavaScript · Updated
JavaScript and Java share some similarities, but it is best to view JavaScript as a new programming language so that you don't confuse the nuances of each language with each other.
JavaScript was initially designed to be used in browsers, but it has evolved to a robust full stack development language that can also run as stand-alone applications on desktops and web servers. We will focus on JavaScript in the browser for the rest of this course.
When you want to go further, JavaScript in Depth teaches the language from the start, with a practice box in every lesson.
Do it: list the snacks in stock
The snack stand's menu shows only the snacks it still has, each as a line of text with its name and price. This one function uses what the chapter taught: an object for each snack, a function, and array methods.
The function gets one value:
snacks: an array of snack objects. Each has three properties:name('Trail Mix'),pricein whole dollars (4) andstock, how many are left (12).
For example:
menuLines([
{ name: 'Trail Mix', price: 4, stock: 12 },
{ name: 'Apple Chips', price: 3, stock: 0 },
{ name: 'Granola Bar', price: 2, stock: 5 }
])
Apple Chips has a stock of 0, so it's left out. The function returns:
['Trail Mix: $4', 'Granola Bar: $2']
Your job: write the function menuLines(snacks) so it returns a new array of text.
- Keep the snacks whose
stockis more than0, in the order they came. - Turn each one into
'<name>:, such aslt;price>' 'Trail Mix: $4'. - When no snack is in stock, or
snacksis empty, return an empty array,[].
Practiced live on the lesson page: write the JavaScript right there and check it.
Sources
Mozilla's MDN Web Docs, the reference most JavaScript developers keep open: