Deli Counter Challenge
Mark Harless
Posted on August 11, 2019
Problem
I’m given three functions to use to help operate a waiting line of people. Customers come in and give their name and are given a number to enter the back of the line. When the first customer in line is being served, they are then removed from the line.
-
takeANumber
adds customer to line. Returns customer’s name and place in line. -
nowServing
announces who they’re serving. Returns customer’s name they’re serving then removing customer from line. -
currentLine
reads people in line. Returns a list of numbered names of all customers in line.
What I Learned
I didn't know
shift()
in a single instance could be used to return and remove the first index. I originally hadshift()
below the return line but I kept getting an error. Then I had it at the bottom of the function but then it would never execute because when ran, it exits the function once it reaches any of the returns.I had to slice the current array into a new array to make an ordered list with all of the customers. I originally overthought this challenge because I wanted to create a very long and complicated concatenated string.
Questions
-
output = output.slice(0, output.length - 2)
removes the comma and space from the last person on the list. I don't understand how because I thoughtoutput.length - 2
would remove the last two indexes from the array, not the last two characters.
Answers
-
var output
is not an array. I originally declared it as one but when I iterated through my For Loop, it changed the array to a string. This meansoutput.length - 2
is removing the last two characters becausevar output
is not an array.
First Iteration
function takeANumber(katzDeliLine, name){
katzDeliLine.push(name);
return `Welcome, ${name}. You are number ${katzDeliLine.length} in line.`;
}
function nowServing(katzDeliLine) {
if (katzDeliLine.length > 0) {
return "Currently serving " + katzDeliLine.shift() + ".";
} else {
return "There is nobody waiting to be served!";
}
}
function currentLine(katzDeliLine) {
var output = [];
if (katzDeliLine.length > 0) {
for(var i = 0; i < katzDeliLine.length; i++) {
output += (i + 1) + ". " + katzDeliLine[i] + ", ";
}
output = output.slice(0, output.length - 2);
return "The line is currently: " + output;
} else {
return "The line is currently empty.";
}
}
Final Iteration
I refactored my code above to make it easier to understand and explain. The only changes that were made is on function currentLine(katzDeliLine)
.
Changed
var output = []
tovar output = ""
and moved it inside the If statement. As stated above, it's not an array.Changed
output = output.slice(0, output.length - 2)
tooutput = output.slice(0, -2)
. It does the same exact thing but is shorter.
function takeANumber(katzDeliLine, name){
katzDeliLine.push(name);
return `Welcome, ${name}. You are number ${katzDeliLine.length} in line.`;
}
function nowServing(katzDeliLine) {
if (katzDeliLine.length > 0) {
return "Currently serving " + katzDeliLine.shift() + ".";
} else {
return "There is nobody waiting to be served!";
}
}
function currentLine(katzDeliLine) {
if (katzDeliLine.length > 0) {
var output = "";
for(var i = 0; i < katzDeliLine.length; i++) {
output += (i + 1) + ". " + katzDeliLine[i] + ", ";
}
output = output.slice(0, -2);
return `The line is currently: ${output}`;
} else {
return "The line is currently empty.";
}
}
Original Problem
A pretty important deli needs somebody to program the "Take a Number" feature for their counter.
At the beginning of the day, the deli is empty and is represented by an empty array, like var katzDeliLine = [];
. However, you don't need to code the array as a variable, since the test scripts will create it and pass it to the functions you are about to build.
Build a function that a new customer will use when entering the deli. The function,
takeANumber
, should accept two parameters: the current line of people, along with the new person's name. The function should return a welcome message including the new person's position in line, such as"Welcome, Ada. You are number 1 in line."
. And don't go being too programmer-y and give them their index. These are normal people. If they are 7th in line, tell them that. Don't get their hopes up by telling them they are number 6 in line.Build a function
nowServing
. This function should accept the current line of people (katzDeliLine
) and return the first person in line and then remove that individual from the line. If there is nobody in line, it should return "There is nobody waiting to be served!"Build a function currentLine that accepts the current line of people and returns the current line as a string; for example, if
katzDeliLine
is currently["Ada", "Grace"]
,currentLine(katzDeliLine)
would return"The line is currently: 1. Ada, 2. Grace"
. You don't have to usekatzDeliLine
as a variable or parameter name in your function though, it's just an example of a variable that might be passed to it. If there is nobody in line, it should return"The line is currently empty."
Acknowledgements
Thanks to Matt Ezell for explaining my Question to me. Thanks to James Montour for helping me refactor my code and showing me how negative numbers work in splice()
.
Posted on August 11, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.