7133

Prompt return as undefined in a function. (scope issue)

Question:

I believe it's a scope issue because I tried setting my function userPrmpt inside firstPartCook and it works. I set it outside, and it doesn't. So it's reading, but not keeping what is returned. I thought by placing it in the test var that it would work, but it doesn't.

Here's my code

<strong>HTML</strong>

<!DOCTYPE html> <html> <head> <title> Race Makin' </title> <!-- Link to the JS portion for this script --> <script src="riceMakin.js"></script> </head> <body> <!-- not going for anything fancy. Just focusing on mechanics -->

Lets cook some damn rice

<h2> To start cooking Rice, please hit start</h2> <button onclick="firstPartCook()">Start</button> <h3>Below explains the status on the Rice Making Machine:</h3> <!-- with JS i have what is inbetween the spans, switching from on and off --> <p id="print">Status: Turned <span id="print">Off</span>

</body> </html>

<strong>JS</strong>

<pre class="lang-js prettyprint-override">//Global Vars here //Promp for the User to continue throught the script. To use what is returned, set to a var function userPrmpt(userResponse) { prompt(userResponse); } // This function is for adding type to the DOM. function insertCopy (copy) { var paragraphCreate = document.createElement("p"); var copyNode = document.createTextNode(copy); paragraphCreate.appendChild(copyNode); var idSelecter = document.getElementById("print"); //print is the id tag of the span idSelecter.appendChild(paragraphCreate); } //This is where we start working on the mechanics of the script function firstPartCook() { //var userAnswer = prompt("Welcome to the Rice Maker, want to start making rice?"); var test = userPrmpt("Hello"); if (test == "yes") { console.log(test); insertCopy("It worked"); } else { console.log(test); insertCopy("Nope"); } }

Answer1:

You have to return the value from the prompt, otherwise the function will just return undefined, which is the default return value of any function that has no other value returned from it

function userPrmpt(userResponse){ return prompt(userResponse); }

Recommend

  • regex to extract a set number of words around a matched word
  • Using vss-web-extension-sdk in Angular 2
  • REST GET requests, verbs and apikey
  • Using vss-web-extension-sdk in Angular 2
  • change keyboard layout with javascript
  • R regmatches() and stringr str_extract() dragging whitespaces along
  • AngularJS and Tab Order (Disabled Buttons)
  • Replacing points of color by a uniform colored surface
  • best backbone.js and require.js boilerplate for huge apps [closed]
  • Prevent focus to URL bar with CTRL + L
  • ASP.NET MVC - Detect Time Spent on Page
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • MAVEN : Run Multiple Maven Project using Maven Test
  • Guava how to copy all files from one directory to another
  • Get a trait object reference from a vector
  • google maps autocomplete bounces back already cleared text …odd…odd…odd
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Outputting SharePoint Hyperlink Column as URL
  • Doctrine/Symfony entity generator and generating entity from one table
  • there is no graph with tensorboard
  • Uncaught TypeError: $(…).select2 is not a function
  • How to view images from protected folder with php?
  • Webgrid not refreshing after delete MVC
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Display images in Django
  • How to have background script and something similar to a default popup?
  • JSON response opens as a file, but I can't access it with JavaScript
  • Play WS (2.2.1): post/put large request
  • Jquery UI tool tip close icon
  • dc-js disable selecting slices on click for pie chart
  • Uncaught Error: Could not find module `ember-load-initializers`
  • req.body is undefined - nodejs
  • Resize panoramic image to fixed size
  • Modifying destination and filename of gulp-svg-sprite
  • Importing jscolor library in angular 2
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Traverse Array and Display in markup
  • Qt: Run a script BEFORE make
  • How to Embed XSL into XML