Creating an array of randomly generated numbers

Wondering what is wrong with the functions aimed to generate three random numbers in the range of 1-10, and displaying them to the console in a string of its own created from an array.


function randNumberX()   {
    1 + Math.floor(10 * Math.random());
}
function genMathDrop()    {
    var randDrop = [
        randNumberX(),
        randNumberX(),
        randNumberX()
    ]
    var randDropStr = randDrop[0] + randDrop[1] + randDrop[2];
    console.log(randDropStr);
}
genMathDrop();

what does your function randNumberX returns?
can you quote the return statement of that function?

1 Like

It is supposed to return a random number in range of 1-10. I don’t know what you mean by quote the return statement.

so, above, you have a function. That function does, in fact, calculate a value between 1 and 10 - but it does nothing with that value. When @ieahleen asked you about the return statement, there’s a point. Every function returns a value, even if we don’t specifically say return. If we don’t return some value, our function still will return something - if we don’t specify, it will return undefined.

So here’s an example:

function myFunctionThatReturnsSomething(){
  const returnedValue = 'I am a string.';
  return returnedValue;
};

This function, when called, returns a value. That value is a string, so we can do

const myStringyThing = myFunctionThatReturnsSomething();
// now, myStringyThing === 'I am a string.'

So, in the function I quoted at the top of this, you have a mathematical expression, but you don’t actually return the value you’ve just calculated. Can you figure how to do that?

For the record, this is exactly the kind of thing the FCC javascript track can help you to learn.

1 Like

Yes I’ve changed the code now, I’ve actually done the FCC JS basics, just still very crude with writing correct syntax as it is still very new to me. I appreciate the help.

It’s the “How do you get to Carnegie Hall?” question: “practice, practice, practice.”

You’re doing fine, and I thought I remembered seeing a few posts from you regarding JS - just keep plugging, and keep asking questions. It takes time to learn, and javascript can be a funny language.

1 Like

Thank you sir. yes I am writing some code to apply the lessons I’ve learnt here.