DEV Community

Mark Harless
Mark Harless

Posted on • Updated on

Deli Counter Challenge

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.

  1. takeANumber adds customer to line. Returns customer’s name and place in line.
  2. nowServing announces who they’re serving. Returns customer’s name they’re serving then removing customer from line.
  3. currentLine reads people in line. Returns a list of numbered names of all customers in line.

What I Learned

  1. I didn't know shift() in a single instance could be used to return and remove the first index. I originally had shift() 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.

  2. 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

  1. 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 thought output.length - 2 would remove the last two indexes from the array, not the last two characters.

Answers

  1. 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 means output.length - 2 is removing the last two characters because var 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.";
    }
}
Enter fullscreen mode Exit fullscreen mode

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).

  1. Changed var output = [] to var output = "" and moved it inside the If statement. As stated above, it's not an array.

  2. Changed output = output.slice(0, output.length - 2) to output = 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.";
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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.

  2. 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!"

  3. 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 use katzDeliLine 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().

Top comments (3)

Collapse
 
aaronfl35987775 profile image
Aaron Flowers

Hi I'm new to coding my name is Aaron. I have some questions on the current line function. I don't understand the variable output and what it's doing. Can you explain that to me line by line please.my email is flowersa29@gmail.com. I'm really kind of stuck and I've only been doing this for about 3 weeks I really appreciate your help thank you

Collapse
 
gregggarrison profile image
gregggarrison

Can you elaborate on output = output.slice(0, -2)? Why would we want to remove the last 2 items?

Collapse
 
miaonanlin profile image
miaonanlin

A simple example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
the output of fruits.slice(0,-2) is ["Banana", "Orange"], which is the first two items in the list.
however, the output of fruits.slice(-2) is ["Apple", "Mango"] , which is the last two items in the list.
.