Guest User

Untitled

a guest
Jun 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. 1. A line of people at an amusement park ride.
  2.  
  3. The line is composed of members, represented as strings.
  4. There is a front to the line, as well as a back.
  5. When someone enters the line, place them at the end.
  6. People may leave the line whenever they see fit, and those behind them take their place.
  7. Given the above real-world information, use an array data structure to code the following solution.
  8.  
  9. a) Use an array input: ["Vivian", "Ava", "Josh", "Patrick", "Mike"]
  10. b) Insert a new person, "Mary" at the end of the line. In other words, you should insert Mary after Mike.
  11. c) Find a person in line named "Josh." This function should return the position of 2 in the array, (recall that arrays are 0-based).
  12. d) Find a person in line named "Emily." What should your function return if it does not find the item in the array?
  13. e) What if Ava wants to allow a friend, "Melissa", to cut in line in front of her? How would you code this so Melissa appears before Ava?
  14. f) If Patrick wants to leave the line, how would you delete him from the array?
  15.  
  16. //a
  17. var peopleInLine = ["Vivian", "Ava", "Josh", "Patrick", "Mike"];
  18.  
  19. function addPerson(lineArray, name) {
  20. lineArray.push(name);
  21. console.log(lineArray);
  22. }
  23. //b
  24. addPerson(peopleInLine, "Mary");
  25.  
  26. function findPerson(array, name) {
  27. for(var i = 0; i < array.length; i++) {
  28. if(peopleInLine[i] === name) {
  29. console.log(i);
  30. return;
  31. }
  32. }
  33. console.log(name + " was not found");
  34. }
  35. //c
  36. findPerson(peopleInLine, "Josh");
  37. //d
  38. findPerson(peopleInLine, "Emily");
  39.  
  40. function cutTheLine(lineArray, personInLine, personCutting) {
  41. for(var i = 0; i < lineArray.length; i++){
  42. if(lineArray[i] === personInLine) {
  43. lineArray.splice(i, 0, personCutting);
  44. lineArray.join();
  45. i++;
  46. }
  47. }
  48. console.log(lineArray);
  49. }
  50. //e
  51. cutTheLine(peopleInLine, "Ava", "Melissa");
  52.  
  53. function removePerson(lineArray, personLeaving) {
  54. for (var i = 0; i < lineArray.length; i++){
  55. if(lineArray[i] === personLeaving){
  56. lineArray.splice(i, 1);
  57. }
  58. }
  59. console.log(lineArray);
  60. }
  61. //f
  62. removePerson(peopleInLine, "Patrick");
  63.  
  64.  
  65.  
  66. 2. Provide another real-world example that you can model using a data structure.
  67. An Ice cream shop can keep the flavors of their ice creams in an array data structure. They would be able to add new flavors, delete flavors.
  68.  
  69. 3. How would your data structure allow developers to access and manipulate the data?
  70. There are functions for different ways to manipulte the data
Add Comment
Please, Sign In to add comment