I'm doing JavaScript... Again


There is a problem and I am a noob so I have no idea how to solve this. I need help please.

I deleted the variables and all that stuff.
I need help on these things.
1 Why is the randint(1, 12) acting weird? I don’t know why
2 I made a function but it has no idea what I am trying to do
3 What block from the text function has the join block that converts a variable to text?
4 How do I do the let item: game.askForNumber(" ") aka how do I make it work and function properly

  1. Before the randint, you have a : instead of an = - instead of saying ‘create random1 and assign it a random number’, it says ‘let item be a variable of type randint(1, 12)’, which isn’t valid. Just swap the : for an equals sign and it will store the variable as expected
  2. Same as above for item, and also the string being passed to ask for number is incorrect - it looks like you’re missing pluses between the strings and variables - e.g. something like"parseint(" + Random1 + ") x parseint(" + Random2 + ")"
  3. Answered above ^ its plus.
  4. Should be combination of the answers for 1 and 2 above.

I’d suggest if you run into issues like this it might be helpful to try making a few simple programs like this in blocks and then switching it over to javascript, to see how that code gets generated / compare and contrast with the code that isn’t working.

1 Like

Thanks, @jwunderl! Now how do I call a function in JavaScript and where should I place the function at? The beginning? The end? I’m pretty sure it’s the beginning.
image
Is this how you call a function?

When I tried doing this, the function never got called and never did the number function variable

Hm, that’s pretty close! It is actually calling the function now, the function just isn’t doing anything - if you return the let item = game.askForNumber( .... ) before the strings in the function (so that the portion where it builds the big string I had mentioned is where the … is), it will prompt the user to enter a number and store the result as item.

Where you put the function in the file doesn’t matter, usually (there’s some subtlety with alternate ways of defining functions and nested functions, but those are both advanced use cases). Note though that in this case that you have to make sure Value1 and Value2 are defined before calling the function. (I would suggest using parameters to make it so this isn’t the case - try making a function with two number parameters in blocks in another project and copy over the javascript for defining that function.

My personal preference is that for ordering my code is that if I have functions and top level code in the same file, I’ll put them at the bottom in roughly the order they get called (e.g. if one function calls another one, I’ll put the one that is called below the one that called it). This way my the code ends up being a bit more high level at the start, and as you scroll down you get more details.

One last thing minor thing, typically in your functions you want to indent the body of the function or conditionals (the stuff between the curly braces {}) by one tab, like this:

function myFunction() {
    console.log("stuff");
}

So that it’s easier to tell which function code belongs to

2 Likes

This is too confusing XD. Maybe I’ll try it in blocks then look at the JavaScript afterward.

1 Like