2

I am trying to create a simple geometry program in JavaScript, but for some reason when I try to run the code in a code runner, it says

Error: prompt is not defined.

Note: I am using a code running app called Code Runner on my Mac.

var question = prompt("What do you want to know?")
if (question = "circumference of circle") {
  var variableCircumferenceCircle = prompt("What is the radius of the circle?")
  return variableCircumferenceCircle
  circumferenceCircle(variableCircumferenceCircle)
}

//Circumference of Circle:
var circumferenceCircle = function(r) {
  console.log("Circumference of Circle = 2 * pi * r")
  console.log("C = 2 * 3.14 * " + r)
  console.log("C = " + 2 * 3.14 * r)
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Tae Rugh
  • 55
  • 1
  • 4

2 Answers2

3

window.prompt() only exists as a host function in the browser.

Other than that environment, if you didn't create it, it will be undefined.

alex
  • 479,566
  • 201
  • 878
  • 984
  • if I can't use prompt, how should I go about asking the user a question and using the user's answer later on in the code? – Tae Rugh Aug 07 '15 at 16:20
  • @TaeRugh try using something like [JS fiddle](https://jsfiddle.net/) if you want to play around, test javascript code. Could be just coderunner doesn't support all or any of javascript's library. – Squeegy Feb 24 '16 at 20:43
-1

You can also write the script at an external source and write static before your script. This makes prompt a global that can be used in multiple places. At least that is what worked for me.

jeff
  • 1
  • 2
    since prompt() is native to javascript, I don't think this would help. It likely has to do with the program they're using to test the code. – Squeegy Feb 24 '16 at 20:40