Advertisement
Guest User

Deli Counter

a guest
Aug 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. Instructions
  2. A pretty important deli needs somebody to program the "Take a Number" feature for their counter.
  3.  
  4. 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.
  5.  
  6. Build a function that a new customer will use when entering the deli. The function, takeANumber, should accept two paramaters: 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.
  7.  
  8. 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!"
  9.  
  10. 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."
  11.  
  12.  
  13. My Code:
  14.  
  15. function takeANumber(line,name){
  16. if(line.indexOf(name) === -1) {
  17. line.push(name);
  18. }
  19. return `Welcome, ${name}. You are number ${line.indexOf(name)+1} in line.`
  20. }
  21.  
  22. function nowServing(line){
  23. if(line[0]){
  24. let firstInLine = line[0];
  25. line.shift();
  26. return `Currently serving ${firstInLine}.`;
  27. } else {
  28. return "There is nobody waiting to be served!"
  29. }
  30.  
  31. }
  32.  
  33. function currentLine(line){
  34. if(line[0]){
  35. let lineNames = "";
  36. for(let i = 0; i < line.length; i++){
  37. if(i === line.length -1){
  38. lineNames += `${i+1}. ${line[i]}`
  39. } else {
  40. lineNames += `${i+1}. ${line[i]}, `
  41. }
  42.  
  43. }
  44. return `The line is currently: ${lineNames}`;
  45. } else {
  46. return "The line is currently empty."
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement