JavaScript Algorithm: Shopping List

Erica N
Erica N
Mar 19 · 2 min read

For today’s algorithm, we are not going to write a function but instead, we are going to learn basic JavaScript fundamentals, mainly the use of math operators.

For this algorithm, you are in charge of ordering food and you plan on ordering a lot of food. The food you’ll buy are:

4 sandwiches
6 salads
5 wraps
10 French fries

The cost of each item per category is:

sandwich - $8.00
salad - $7.00
wrap - $6.50
french fries - $1.20

The goal of this function is to output the total cost of your order.

We start out by creating four variables of each of the food categories. The cost times the quantity of each food item is assigned to the variables.

const sandwiches = 4 * 8.00;
const salads = 6 * 7.00;
const wraps = 5 * 6.50;
const frenchFries = 10 * 1.20;

Next, we add up all the food items and assign it to a variable called totalPrice.

let totalPrice = sandwiches + salads + wraps + frenchFries;

The function will output the total which is 118.5. That’s it. Hopefully, you understand what’s going on and how we got to this total.

Erica N

Written by

Erica N

An illustrator that solves easy programming algorithms while writing about humorous articles about nothing.

JavaScript in Plain English

Learn the web's most important programming language.

More From Medium

More from JavaScript in Plain English

More from JavaScript in Plain English

More from JavaScript in Plain English

32 funny Code Comments that people actually wrote


More from JavaScript in Plain English

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade