Skip to content

Commit

Permalink
refactor(api, curriculum): use the shared shuffleArray util (freeCode…
Browse files Browse the repository at this point in the history
…Camp#56444)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
  • Loading branch information
huyenltnguyen and ojeytonwilliams authored Oct 2, 2024
1 parent 52ea949 commit 0ba9eef
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 42 deletions.
16 changes: 1 addition & 15 deletions api-server/src/server/utils/exam.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
function shuffleArray(arr) {
let currentIndex = arr.length,
randomIndex;

while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[arr[currentIndex], arr[randomIndex]] = [
arr[randomIndex],
arr[currentIndex]
];
}

return arr;
}
import { shuffleArray } from '../../../../shared/utils/shuffle-array';

function filterDeprecated(arr) {
return arr.filter(i => !i.deprecated);
Expand Down
18 changes: 1 addition & 17 deletions api/src/utils/exam.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
import { Exam, Question } from '@prisma/client';
import { shuffleArray } from './../../../shared/utils/shuffle-array';
import { UserExam, GeneratedExam } from './exam-types';

function shuffleArray<T>(arr: T[]): T[] {
let currentIndex: number = arr.length;
let randomIndex: number;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;

[arr[currentIndex], arr[randomIndex]] = [
arr[randomIndex] as T,
arr[currentIndex] as T
];
}

return arr;
}

/**
* Remove objects from array with deprecated: true.
*
Expand Down
11 changes: 2 additions & 9 deletions curriculum/test/utils/sort-challenges.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { shuffleArray } = require('../../../shared/utils/shuffle-array');
const { sortChallenges } = require('./sort-challenges');

const challenges = [
Expand Down Expand Up @@ -222,7 +223,7 @@ const challenges = [
describe('sortChallenges', () => {
it('sorts challenges by superblock, block and challenge order', () => {
const copyOfChallenges = [...challenges];
shuffle(copyOfChallenges);
shuffleArray(copyOfChallenges);
const actualChallenges = sortChallenges(copyOfChallenges);

expect(actualChallenges).toEqual(challenges);
Expand All @@ -241,11 +242,3 @@ describe('sortChallenges', () => {
expect(actualChallenges[0]).not.toEqual(copyOfChallenges[0]);
});
});

// Use the Fisher–Yates shuffle algorithm to shuffle array
const shuffle = arr => {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
};
5 changes: 4 additions & 1 deletion shared/utils/shuffle-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export const shuffleArray = <T>(arrToShuffle: Array<T>) => {

for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];

// We know that i and j are within the bounds of the array, TS doesn't
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
[arr[i], arr[j]] = [arr[j]!, arr[i]!];
}

return arr;
Expand Down

0 comments on commit 0ba9eef

Please sign in to comment.