JavaScript Algorithm: Electronics Shop

Erica N
Erica N
Dec 23, 2019 · 4 min read

For today’s algorithm, we are going to write a function called getMoneySpent that will take in three inputs: two arrays keyboards and drives and an integer b.

You want to buy a keyboard and a USB drive so you go to your local electronics store to get them. The store has several models of keyboards and USB drives. You want to buy them all but too bad, you have a thing called a budget. You want to spend as much as possible for the keyboard and USB drive while remaining within budget.

Given an array of keyboard prices and an array of USB drive prices, the purpose of the function is to output the most amount of money you can spend when buying a keyboard and a USB drive together. If you can only afford only a keyboard or only a USB but not both together, you output -1. Let’s look at an example.

let b = 10;
let keyboards = [3, 1];
let drives = [5, 2, 8];

The function will take in three inputs:

b is your budget total.

keyboards is the array holding the prices of two keyboards.

drives is the array holding the prices of three USB drives.

We add the price of each keyboard to each USB drive:

// the first keyboard
3 + 5 = 8
3 + 2 = 5
3 + 8 = 11
// second keyboard
1 + 5 = 6
1 + 2 = 3
1 + 8 = 9

By looking at the calculations, we took the price of the first keyboard in the keyboard array and add it to the price of each USB drive. We did the same with the price of the second keyboard. The budget is 10, so any number that is greater than 10 is outside the budget. We look at the highest number that is equal or less than 10 and see that we can buy the second keyboard and the third USB drive for $9. The function would output 9.

Now we can turn this into code. We create our variables.

let priceCheck = 0;

The priceCheck variable will hold the most amount of money we can spend within our budget. This will be the variable the function will output. We set the value to 0.

Next, we use a for-loop but not just one we use two for-loops one inside the other. This is called a nested for-loop

for (let i = 0; i < keyboards.length; i++) {
for (let j = 0; j < drives.length; j++) {
let priceTotal = keyboards[i] + drives[j]
if ((priceTotal > priceCheck) && (priceTotal <= b)) {
priceCheck = priceTotal;
}
}
}

Now we use a for-loop to loop through the keyboards array and inside that for-loop, we nest a for-loop inside. This for-loop will iterate through the drives array. Which loop is the outer loop and the inner loop doesn’t matter. You can make the outer loop iterate through the drives array and the inner loop iterate through the keyboards array.

What the nested loops are doing is when we iterate through the keyboards array, we take the first number in the array and then go into the inner loop and begin adding that first keyboard array item to all the items in the drive array. This happens each time before starting over onto the next keyboard array item.

We add the cost of each keyboard and USB drive to the priceTotal variable. We use the if-statement to check if the cost of the keyboard and the USB drive is greater than priceCheck but also see if it is within budget. If it is, update priceCheck to equal to the current priceTotal variable. As the for-loop iterates through each item, the priceCheck variable will continue to update. Each value will become larger and larger until it can’t become any larger without going outside the budget.

if (priceCheck === 0) {
return -1;
} else {
return priceCheck;
}

When the for-loop ends, we check to see if priceCheck remained 0 or if it has another value. If it is still a 0, that means none of the keyboard and USB drive price combinations are within budget so return -1. If there is a value, then that means you can actually afford something. The function outputs the most amount you can spend.

That concludes our code. Here is the rest of the function:

function getMoneySpent(keyboards, drives, b) {
let priceCheck = 0;
for (let i = 0; i < keyboards.length; i++) {
for (let j = 0; j < drives.length; j++) {
let priceTotal = keyboards[i] + drives[j]
if ((priceTotal > priceCheck) && (priceTotal <= b)) {
priceCheck = priceTotal;
}
}
}
if (priceCheck === 0) {
return -1;
} else {
return priceCheck;
}
}

JavaScript in Plain English

Learn the web's most important programming language.

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

More from JavaScript in Plain English

32 funny Code Comments that people actually wrote

10.4K

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