A quiz app shows its questions in a random order. To make tests repeatable, the random numbers are passed in: randoms holds numbers from 0 (included) to 1 (not included), to be used one at a time, in order.
Write shuffleQuiz(questions, randoms) that returns a new shuffled array using this exact procedure: copy questions; then for i from the last index down to 1, take the next random number r, set j = Math.floor(r * (i + 1)), and swap the items at i and j.
Input: questions = ["q1","q2","q3","q4"], randoms = [0.5,0.9,0.1] Output: ["q2","q1","q4","q3"]
i = 3: j = 2, swap to get q1, q2, q4, q3. i = 2: j = 2, no change. i = 1: j = 0, swap to get q2, q1, q4, q3.
Input: questions = ["only"], randoms = [] Output: ["only"]
One question has only one possible order.
0 <= questions.length <= 10^5randoms.length === Math.max(0, questions.length - 1)0 <= randoms[i] < 1Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #86 Shuffle the Quiz Questions. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.