Deli Counter Challenge
Tags: javascript, array
Published: 2019-08-11 16:26:48

## 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

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

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.

```javascript
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.

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](https://blog.immatt.com/) for explaining my Question to me. Thanks to [James Montour](https://twitter.com/JamesMontour) for helping me refactor my code and showing me how negative numbers work in `splice()`.