From aa78ee52ae1d2d4ebfce7ec2c0570b76889c43cd Mon Sep 17 00:00:00 2001 From: Ajay A Date: Fri, 20 Jun 2025 16:40:28 +0530 Subject: [PATCH] feat(curriculum): update lunch picker test to prevent mutation (#60145) Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com> --- .../66db529d37ad966480ebb633.md | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/curriculum/challenges/english/25-front-end-development/lab-lunch-picker-program/66db529d37ad966480ebb633.md b/curriculum/challenges/english/25-front-end-development/lab-lunch-picker-program/66db529d37ad966480ebb633.md index cd5d4fb19bb..cd1f1aef62a 100644 --- a/curriculum/challenges/english/25-front-end-development/lab-lunch-picker-program/66db529d37ad966480ebb633.md +++ b/curriculum/challenges/english/25-front-end-development/lab-lunch-picker-program/66db529d37ad966480ebb633.md @@ -307,29 +307,57 @@ const temp = console.log; try { console.log = obj => tempArr.push(obj); - // check that it correctly outputs the first item Math.random = () => 0; - getRandomLunch(testLunches); + getRandomLunch([...testLunches]); assert.strictEqual(tempArr[0], `Randomly selected lunch: ${testLunches[0]}`); - // second item + // Second item Math.random = () => 0.4; - getRandomLunch(testLunches); + getRandomLunch([...testLunches]); assert.strictEqual(tempArr[1], `Randomly selected lunch: ${testLunches[1]}`); - // third item + // Third item Math.random = () => 0.8; - getRandomLunch(testLunches); + getRandomLunch([...testLunches]); assert.strictEqual(tempArr[2], `Randomly selected lunch: ${testLunches[2]}`); } finally { - // restore Math.random Math.random = tempRandom; console.log = temp; } ``` +The `getRandomLunch` function should not change the array passed to it as argument. + +```js +const testLunches = ["Fish", "Fries", "Roast"]; +let originalLunches = [...testLunches]; +const tempRandom = Math.random; + +try { + Math.random = () => 0; + getRandomLunch(testLunches); + assert.sameOrderedMembers(testLunches, originalLunches); + + Math.random = () => 0.4; + getRandomLunch(testLunches); + assert.sameOrderedMembers(testLunches, originalLunches); + + Math.random = () => 0.8; + getRandomLunch(testLunches); + assert.sameOrderedMembers(testLunches, originalLunches); + + const emptyLunches = []; + let originalEmpty = [...emptyLunches]; + getRandomLunch(emptyLunches); + assert.sameOrderedMembers(emptyLunches, originalEmpty); + +} finally { + Math.random = tempRandom; +} +``` + You should define a function `showLunchMenu`. ```js