I would like to inquire about JavaScript return.

Asked 2 years ago, Updated 2 years ago, 133 views

<script>
function showMovie(age) {
    //Exit if checkAge return value is false
    if (!checkAge(age)) { 
        return alert ('Error');
    //The return value of checkAge is true
    } } else { 
        alert('movie screening');
    }
}

function checkAge(age) {
    if (age > 18) {
        return true; 
    } } else {
        //Return false if you do not agree
        Did you get the consent of your guardian?'); 
    }
}

function init() {
    letage = prompt ('Please tell me your age');
    showMovie();
}
init();
</script>

If you enter a value that is over 18 years old, you should get a movie screening message right away. I wonder why it is moved to the confirm message of the protection automatic.

javascript if-else

2022-09-21 13:51

1 Answers

Just a quick look at the doctor's code:

call showMove()
call checkAge(age) // from here, age is undefined
Ifage > 18 //age is undefined and undefined is NaN, so it must be false
Did you get the consent of your guardian?')

That's what happens.

This is because age is not a global variable, but a regional variable in init(), so it cannot be referenced in other functions.


2022-09-21 13:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.