-10

I'll try to explain as best as I can because I cannot find this anywhere.

I'm making a simple dice rolling script.

function rollDice(number, sides) {
    return("Rolled (number) (sides) sided dice")
}

so when I type the command for an example rollDice(1, 5) I want it to input "Rolled one five sided dice" but I don't know how to input the (number, sides) into the return command.

EDIT: The question has been answered, I used this

var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty'];

function rollDice(number, sides) {
    return("Rolled "+ names[number] +" "+ names[sides] +" sided dice");
}

to convert the numbers into words and to input (number, sides) into the 'return' command.

If anyone will look back at this tho, I'm having another problem, I want to input arguments of a previous function inside my new function, but it's returning an error:

function randomNum(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomDice(number, sides) {
    randomNum(+number+, +sides+);
}

randomDice(1, 5)

the part that gives me the error is:

function randomDice(number, sides) {
    randomNum(+number+, +sides+);
}

Thank you for the help

Frankie
  • 3
  • 4

2 Answers2

-2

You can use Template Literals. Also you will need to have a mapping of 1 to one and so on..

function rollDice(number, sides) {
  const mapping = ['one', 'two', 'three', 'four', 'five', 'six'];
  return (`Rolled ${mapping[number-1]} ${mapping[sides-1]} sided dice`);
}

console.log(rollDice(1, 5));
void
  • 36,090
  • 8
  • 62
  • 107
-2

Its so easy to input these parameters to the return statement just use + operator before and after parameters as below:-

function rollDice(number, sides) {
    return "Rolled " + number +  " " + sides +" sided dice";
}

and also you can change integers to string using integerValue.toString() for example 5.toString() or for your code number.toString().

Azhy
  • 704
  • 3
  • 16