-1
                var confirmation = confirm("Are you sure you want to enter your name?");
                
                function confirmTrue() {
                    var nameInput = prompt("Great! Now enter your name:");
                    return nameInput;
                }
                
                if (confirmation == true) {
                    confirmTrue();
                } else {
                    alert("I was hoping for a response..");
                }
                
                if (confirmTrue != null || confirmTrue != "") {
                    alert("Hello, " + window.nameInput);
                }
            }
            
            confirmCallName();

i want to make a function where you confirm something to type your name in, and then after the input, it will greet you with the input result. but when the prompt returns true or false, there is a final alert that says "Hello, undefined". how do I make it's so that it doesnt activate when we cancel the prompt and after I confirm the input?

someone told me that the variable is inside the function. if so, how do I make it global?

  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – ASDFGerte Apr 07 '22 at 13:45
  • Also, my glass ball is telling me whatever tutorial you are reading, it's bad. – ASDFGerte Apr 07 '22 at 13:46
  • Just store the return value of `confirmTrue` into a variable (local to `confirmCallName`) and move the second `if` into the first `if`, so that you don't alert the name if the user chose No in the first dialog. – FZs Apr 07 '22 at 13:58

1 Answers1

0

I made a couple of modifications to get this working how I think you want it. The code is below. You need to assign the value of the confirmTrue result to a variable and then test that. Also, you need to add the case to handle cancelling that result.

var confirmation = confirm("Are you sure you want to enter your name?");
                
                function confirmTrue() {
                    var nameInput = prompt("Great! Now enter your name:");
                    return nameInput;
                }
                
                if (confirmation == true) {
                    var getInput = confirmTrue();
                    if (getInput) {
                        alert("Hello, " + getInput);
                    } else {
                        alert("You cancelled your input.");
                    }
                } else {
                    alert("I was hoping for a response..");
                }
Mifo
  • 563
  • 6
  • 6