0

Trying to get this below to generate a random name made up of two parts and pushed together, I need help making it so that it prints out to the screen and so that it will chose one of the three options every time as at the moment sometimes it chooses none. I'm new to JavaScript.

const randomName = {
   prefix: ['Bramble', 'Lion', 'Owl'],
   suffix: ['Paw', 'Throat', 'Tail']
};

let firstHalf = () => {
switch(Math.floor(Math.random() * 10)){
  case 0:
      console.log(randomName.prefix[0])
  break;
  case 1:
      console.log(randomName.prefix[1])
  break;
  case 2:
      console.log(randomName.prefix[2])
  break;
};
};

let secondHalf = () => {
switch(Math.floor(Math.random() * 10)){
    case 0:
        console.log(randomName.suffix[0])
    break;
    case 1:
        console.log(randomName.suffix[1])
    break;
    case 2:
        console.log(randomName.suffix[2])
    break;
  };
};


console.log(`Your warrior name is ${firstHalf()} ${secondHalf()}!`);
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Daragca
  • 16
  • 1
  • 1
    Your switch case returns a number between 0 and 9. If you need a number between a min and a max value you should check: [Random number between two numbers](https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript) – Reyno Jul 05 '21 at 13:15

5 Answers5

1

Simple way is create function which return one of the element of array without any case switch loops

const randomName = {
   prefix: ['Bramble', 'Lion', 'Owl'],
   suffix: ['Paw', 'Throat', 'Tail']
};
function getRand(arr){
  return arr[Math.floor(Math.random()*arr.length)]
}
console.log(`Your warrior name is ${getRand(randomName.prefix)} ${getRand(randomName.suffix)}!`);
Greg--
  • 636
  • 4
  • 16
  • You are using `arr.length` to define wich maximum `index` can be choosen, but the maximum index is 1 less than the array length since the first index is 0. using `Math.floor` will result in having very small chances of that happening but it can still happen if the output of `Math.random` is 1. – Shyne Jul 05 '21 at 13:38
  • Shyne is incorrect. The `Math.floor` approach here will give a proper even distribution. – Cerbrus Jul 05 '21 at 13:45
  • @Shyne please read documentation about [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) in this example you get from 0 to 2, that exactly what he need – Greg-- Jul 05 '21 at 13:53
  • My bad, i thought `Math.random` would return a number between 0 and 1 inclusive but 1 is excluded. – Shyne Jul 05 '21 at 14:05
0

Math.random() randomly picks in [0;1[ if you do x10 you end up with this range [0;10[ but you only have 3 values (indexes 0, 1 and 2) so when it picks 3 or above you end up with nothing picked.

You need to find a way to pick [0,3[ to make sure you have a value that correspond to your random number

Apolo
  • 3,844
  • 1
  • 21
  • 51
0

There are several problems in your code:

  1. firstHalf and secondHalf do not return anything.
  2. Your Math.floor(Math.random() * 10) results in random numbers between 0 and 9, while you have only 3 options. Replace that 10 with 3`. Or... use the array's length.
  3. You don't need a switch statement if the condition of the switch is what you use:
switch(myNumber){
  case 0:
    doStuffWith[0];
  break;
  case 1:
    doStuffWith[1];
  break;
  case 2:
    doStuffWith[2];
  break;
};

Can be replaced with:

doStuffWith[myNumber]

So, that all together, firstHalf should probably be something like:

let firstHalf = () => {
  const name = randomName.prefix[Math.floor(Math.random() * randomName.prefix.length)];
  console.log('firstHalf', name);
  return name;
};
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

If you use a function which returns a number between a min and max you could do the following. Check the following link @Reyno mentioned in the comments to find a function like this: Generate random number between two numbers in JavaScript

As Cerbrus somehow already wrote:

If the function then only returns numbers which are represented in your name-arrays you don't need the switch case.

const randomName = {
  prefix: ['Bramble', 'Lion', 'Owl'],
  suffix: ['Paw', 'Throat', 'Tail']
};

firstHalf = randomName.prefix[randomNumberBetween(0, 3)];
secondHalf = randomName.suffix[randomNumberBetween(0, 3)];

Or to make it more relaiable use the length of your name arrays so you don't always have to change the max value if you add new names

const randomName = {
  prefix: ['Bramble', 'Lion', 'Owl'],
  suffix: ['Paw', 'Throat', 'Tail']
};

firstHalf = randomName.prefix[randomNumberBetween(0, randName.prefix.length)];
secondHalf = randomName.suffix[randomNumberBetween(0, randName.suffix.length)];
db3000
  • 400
  • 5
  • 16
-1

you are doing it wrong, don't use switch statements and individually coding each value, that's very bad practice. now, your random function is wrong too, you want a integer between 0 and 2 and generate a FLOAT between 0 and 10. here is what you should do to generate your random number:

function random(min, max) {
    let delta = max - min;
    return min+Math.round(Math.random()*delta);
}

and to get a random value in an array you can do this:

let firstHalf = randomName.prefix[random(0, randomName.prefix.length-1)];
Shyne
  • 192
  • 1
  • 9
  • 1
    `Math.floor` was perfectly fine. In fact, your `Math.Round` can result in numbers 1 higher than `max`. – Cerbrus Jul 05 '21 at 13:21
  • `Math.round` cannot result in numbers 1higher than max. example with 2 as min and 5as max and random value being the maximum: delta = 5 - 2 = 3 | return value = 2 + 1*3 = 5 rounding 5 will always result in 5. And `Math.floor` will prevent us from having the max value and probabilities of having the different numbers won't be equal. – Shyne Jul 05 '21 at 13:31
  • Incorrect. The `Math.round` approach is in fact the one that gives an [uneven distribution](https://stackoverflow.com/a/7377769/1835379) – Cerbrus Jul 05 '21 at 13:44
  • After checking the link you sent i still can tell you that `math.floor` is uneven because if you have a range of [0, 100] 0 will have more chances of being picked than 100. and if you use `Math.round` they have the same chances of being picked. – Shyne Jul 05 '21 at 13:49
  • Take this scenario: `Math.random() * 10`. If that rolls `0.0`, then the resulting `0.0` will floor to `0`. If that rolls `0.99`, then the resulting `9.9` will floor to `9`. That's a perfect _inclusive_ range of `10` integers, from `0` to `9`. You're getting confused by the fact that the `floor` approach is zero-based. – Cerbrus Jul 05 '21 at 13:52
  • ok, `floor` don't confuse me at all, i just thought that `Math.random` sent back an number between 0 and 1 inclusive but as @Greg-- pointed out, it is excluded my bad, but using Math.round will still not generate numbers above max, though. – Shyne Jul 05 '21 at 14:04