这个函数在 js 中的最终结果存在一些问题

some problems with the final result this function in js

                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();

我想做一个功能,让你确认输入你的名字,然后在输入之后,它会用输入结果问候你。但是当提示 returns true 或 false 时,会有一个最后的警告说“你好,未定义”。如何让它在我们取消提示和确认输入后不激活?

有人告诉我变量在函数内部。如果是这样,我如何使其成为全球性的?

我做了一些修改,使它按照我认为你想要的方式工作。代码如下。您需要将 confirmTrue 结果的值分配给一个变量,然后对其进行测试。此外,您需要添加案例来处理取消该结果。

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..");
                }