Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. /**
  2. * Seven students A, B, C, D, E, F and G are sitting in a row.
  3. * G is between A and E.
  4. * F and A have one student between them.
  5. * E and C have two students between them.
  6. * D is immediate right of F.
  7. * C and B have three students between them.
  8. */
  9.  
  10. const swap = (array, a, b) => {
  11. [array[a], array[b]] = [array[b], array[a]]
  12. }
  13.  
  14. let row = ["A", "B", "C", "D", "E", "F", "G"]
  15.  
  16. // G is between A and E
  17. swap(row, row.indexOf("A"), row.indexOf("G") + 1)
  18. swap(row, row.indexOf("E"), row.indexOf("G") - 1)
  19.  
  20. // F and A have one student between them
  21. swap(row, row.indexOf("F"), row.indexOf("A") + 2)
  22.  
  23. // E and C have two students between them
  24. swap(row, row.indexOf("C"), row.indexOf("A") + 1)
  25.  
  26. // D is to the immediate right of F
  27. swap(row, row.indexOf("D"), row.indexOf("F") + 1)
  28.  
  29. row = row.filter(Boolean)
  30.  
  31. console.log("Who is sitting in the middle? - ", row[Math.floor(row.length / 2)])
  32. console.log("Sequence of the students - ", row.join(" "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement