Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Seven students A, B, C, D, E, F and G are sitting in a row.
- * G is between A and E.
- * F and A have one student between them.
- * E and C have two students between them.
- * D is immediate right of F.
- * C and B have three students between them.
- */
- const swap = (array, a, b) => {
- [array[a], array[b]] = [array[b], array[a]]
- }
- let row = ["A", "B", "C", "D", "E", "F", "G"]
- // G is between A and E
- swap(row, row.indexOf("A"), row.indexOf("G") + 1)
- swap(row, row.indexOf("E"), row.indexOf("G") - 1)
- // F and A have one student between them
- swap(row, row.indexOf("F"), row.indexOf("A") + 2)
- // E and C have two students between them
- swap(row, row.indexOf("C"), row.indexOf("A") + 1)
- // D is to the immediate right of F
- swap(row, row.indexOf("D"), row.indexOf("F") + 1)
- row = row.filter(Boolean)
- console.log("Who is sitting in the middle? - ", row[Math.floor(row.length / 2)])
- console.log("Sequence of the students - ", row.join(" "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement