Need help with a random code snippet I was writing

Hi I cant seem to understand why this code snippet prints an undefined alongside the value? Can someone help me?

const randomRangedWrapper = () => {
let randomNumberRanged = Math.floor(Math.random() * 10);
switch(randomNumberRanged) {
case 1:
console.log(‘Your weapon slips from your hand suddenly numb’);
break;
case 2:
console.log(‘A stone falls from nowhere striking you in the head midswing, you collapse to the ground prone.’);
break;
case 3:
console.log(‘A passing bird poops in your eyes, you are blinded for one turn and drop your weapon.’);
break;
case 4:
console.log(‘A portal opens in the air in front of you as a fist belonging to some creature hits you in the face, take 1D6 + 1 Bludgeoning damage. What are the chances?’);
break;
case 5:
console.log(‘You trip on something and stub your toe, take 1D4 bludgeoning damage.’);
break;
case 6:
console.log(‘You pull a muscle in your leg and are hamstringed, your speed is halved for 2 turns.’);
break;
case 7:
console.log(‘You over-extend during your attack, your opponent has advantgage on their next attack roll’);
break;
case 8:
console.log(‘You swing wildly causing your weapon to slide from your grasp and fall 10 feet from you in a random direction’);
break;
case 9:
console.log(‘A dark elf pops out of a time portal and smacks you upside the head, you are stunned for two turns.’);
break;
case 10:
console.log(‘Your weapon snags on your clothing,armor or body and injures your weapon arm, you have disadvantage on the next attack with that arm.’)
break;
default:
console.log(‘looks like nothing bad happened, that was a close one!’)
}
}
console.log(randomRangedWrapper())

I am writing a generator in a forked code project

Notice how you’re console.loging the function call? Since the function doesn’t explicitly return anything (you don’t have any return statements in the function), the function has a return value of undefined. When you print the function call, any console.logs inside it run, as well as the return value (undefined) being printed.

That makes sense thanks, I cleaned it up and got that to go away but now its sending me a number as well when logging
const randomRangedWrapper = () => {

switch(randomVal = Math.floor(Math.random() * 10)) {

case 1:

 console.log('Your weapon slips from your hand suddenly numb');

break; 

case 2:

console.log('A stone falls from nowhere striking you in the head midswing, you collapse to the ground prone.');

break;

case 3:

console.log('A passing bird poops in your eyes, you are blinded for one turn and drop your weapon.');

break;

case 4:

console.log('A portal opens in the air in front of you as a fist belonging to some creature hits you in the face, take 1D6 + 1 Bludgeoning damage. What are the chances?');

break;

case 5:

console.log('You trip on something and stub your toe, take 1D4 bludgeoning damage.');

break;

case 6:

console.log('You pull a muscle in your leg and are hamstringed, your speed is halved for 2 turns.');

break;

case 7:

console.log(‘You over-extend during your attack, your opponent has advantgage on their next attack roll’);

break;

case 8:

console.log(‘You swing wildly causing your weapon to slide from your grasp and fall 10 feet from you in a random direction’);

break;

case 9:

console.log('A dark elf pops out of a time portal and smacks you upside the head, you are stunned for two turns.');

break;

case 10:

console.log('Your weapon snags on your clothing,armor or body and injures your weapon arm, you have disadvantage on the next attack with that arm.')

break;

default:

console.log('looks like nothing bad happened, that was a close one!')

}

return randomVal

}

console.log(randomRangedWrapper());

Is this also because im just returning the switch value which is also the math.random value? If so how does anyone return a switch statement with a method wrapper that doesn’t include the number generated? any help is much appreciated! :smiley:

Aside

Notice how the code portion is relatively simple and straight forward but the bulk is made up by string data? That is why we use data stores… So the bulky text is not part of our source code. This is the first level of abstraction. Now we can see the logic.

Since you are using an ordered switch, it follows that a useful data store would be an array, given its ordered index.

const mishaps = [
  'looks like nothing bad happened, that was a close one!',
  'Your weapon slips from your hand suddenly numb',
  'A stone falls from nowhere striking you in the head midswing, you collapse to the ground prone.',
  'A passing bird poops in your eyes, you are blinded for one turn and drop your weapon.',
  'A portal opens in the air in front of you as a fist belonging to some creature hits you in the face, take 1D6 + 1 Bludgeoning damage. What are the chances?',
  'You trip on something and stub your toe, take 1D4 bludgeoning damage.',
  'You pull a muscle in your leg and are hamstringed, your speed is halved for 2 turns.',
  'You over-extend during your attack, your opponent has advantgage on their next attack roll',
  'You swing wildly causing your weapon to slide from your grasp and fall 10 feet from you in a random direction',
  'A dark elf pops out of a time portal and smacks you upside the head, you are stunned for two turns.',
  'Your weapon snags on your clothing,armor or body and injures your weapon arm, you have disadvantage on the next attack with that arm.'
]

Now the logic…

const getRandText = function (arr) {
  return `* ${arr[Math.floor(Math.random() * arr.length)]}\n`
}

Invoke it with any array of text elements…

console.log(getRandText(mishaps))

Possible output…

* looks like nothing bad happened, that was a close one!

Notice how we can now add to or remove from the data store and never have to maintain the logic in the process?

See how the logic is stable and portable? We have total separation from the data, apart from the type dependency. The key words are ‘any array of text elements’.

Yeah that makes a ton more sense then what I am trying to do. Thanks!

1 Like