From 854dc5d53215444ef35ea6711dc8af65de077d89 Mon Sep 17 00:00:00 2001 From: Mr Footwork Date: Wed, 23 Oct 2024 01:17:23 +0200 Subject: [PATCH 1/2] iteration 2 done --- src/clue.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 8 deletions(-) diff --git a/src/clue.js b/src/clue.js index 58d8608ab..f19e73f0a 100644 --- a/src/clue.js +++ b/src/clue.js @@ -2,25 +2,117 @@ // Suspects Array -const suspectsArray = []; +const suspectsArray = [ + { + firstName: 'Jacob', + lastName: 'Green', + occupation: 'Entrepreneur', + age: 45, + description: 'He has a lot of connections', + image: + 'https://pbs.twimg.com/profile_images/506787499331428352/65jTv2uC.jpeg', + color: 'green', + }, + { + firstName: 'Doctor', + lastName: 'Orchid', + occupation: 'Scientist', + age: 26, + description: 'PhD in plant toxicology. Adopted daughter of Mr. Boddy', + image: 'http://www.radiotimes.com/uploads/images/Original/111967.jpg', + color: 'white', + }, + { + firstName: 'Victor', + lastName: 'Plum', + occupation: 'Designer', + age: 22, + description: 'Billionaire video game designer', + image: + 'https://66.media.tumblr.com/ee7155882178f73b3781603f0908617c/tumblr_phhxc7EhPJ1w5fh03_540.jpg', + color: 'purple', + }, + { + firstName: 'Kasandra', + lastName: 'Scarlet', + occupation: 'Actor', + age: 31, + description: 'She is an A-list movie star with a dark past', + image: 'https://www.radiotimes.com/uploads/images/Original/111967.jpg', + color: 'red', + }, + { + firstName: 'Eleanor', + lastName: 'Peacock', + occupation: 'Socialité', + age: 36, + description: + 'She is from a wealthy family and uses her status and money to earn popularity', + image: 'https://metrouk2.files.wordpress.com/2016/07/mrs-peacock.jpg', + color: 'blue', + }, + { + firstName: 'Jack', + lastName: 'Mustard', + occupation: 'Retired Football player', + age: 62, + description: + 'He is a former football player who tries to get by on his former glory', + image: + 'https://static.independent.co.uk/s3fs-public/thumbnails/image/2016/07/04/08/unspecified-3.jpg', + color: 'yellow', + }, +] // Rooms Array -const roomsArray = []; +const roomsArray = [ + { name: 'Dining Room' }, + { name: 'Conservatory' }, + { name: 'Kitchen' }, + { name: 'Study' }, + { name: 'Library' }, + { name: 'Billiard Room' }, + { name: 'Lounge' }, + { name: 'Ballroom' }, + { name: 'Hall' }, + { name: 'Spa' }, + { name: 'Living Room' }, + { name: 'Observatory' }, + { name: 'Theater' }, + { name: 'Guest House' }, + { name: 'Patio' }, +] // Weapons Array -const weaponsArray = []; - +const weaponsArray = [ + { name: 'rope', weight: 10 }, + { name: 'knife', weight: 8 }, + { name: 'candlestick', weight: 2 }, + { name: 'dumbbell', weight: 30 }, + { name: 'poison', weight: 2 }, + { name: 'axe', weight: 15 }, + { name: 'bat', weight: 13 }, + { name: 'trophy', weight: 25 }, + { name: 'pistol', weight: 20 }, +] // ITERATION 2 -function selectRandom() {} - -function pickMystery() {} +function selectRandom(arr) { + const randomIndex = Math.floor(Math.random() * arr.length) + return arr[randomIndex] +} +function pickMystery() { + return { + suspect: selectRandom(suspectsArray), + weapon: selectRandom(weaponsArray), + room: selectRandom(roomsArray), + } +} // ITERATION 3 function revealMystery() {} - From 1da6821fb3035347da198e3cd18b9fd37c67c8b7 Mon Sep 17 00:00:00 2001 From: Mr Footwork Date: Wed, 23 Oct 2024 01:36:49 +0200 Subject: [PATCH 2/2] Solved lab --- src/clue.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/clue.js b/src/clue.js index f19e73f0a..f7bad65ef 100644 --- a/src/clue.js +++ b/src/clue.js @@ -101,6 +101,11 @@ const weaponsArray = [ // ITERATION 2 function selectRandom(arr) { + if (!arr.length) { + console.warn('Stack is empty and does not contain any cards.') + return undefined + } + const randomIndex = Math.floor(Math.random() * arr.length) return arr[randomIndex] } @@ -115,4 +120,12 @@ function pickMystery() { // ITERATION 3 -function revealMystery() {} +function revealMystery(envelope) { + if (!envelope.suspect || !envelope.weapon || !envelope.room) { + throw new Error( + 'The envelop object must be of shape { suspect, weapon, room }' + ) + } + + return `${envelope.suspect.firstName} ${envelope.suspect.lastName} killed Mr. Boddy using the ${envelope.weapon.name} in the ${envelope.room.name}!` +}