简体   繁体   中英

Writing a function to call other functions without calling on the user prompts

I'm trying to write a function that calls others function to produce a final answer. However, if I try to call halfNumber and squarenumber functions, I'm also going to be calling the individual prompts from those functions, and I don't want that to happen. Is there a way I can restructure my code so, I can be able to call crazyFunction without calling the prompts from the other functions?

Javascript:

var el1 = document.getElementById('squarenum');
el1.onclick = squarenumber;

var el2 = document.getElementById('halfnum');
el2.onclick = halfNumber;

function squarenumber(n){

    n = prompt("Enter a number to be squared");
    var result = n * n;


    var txt = document.getElementById('square_answer');
    txt.innerHTML = "The result of squaring the number " + n + " is " + result; 
    return result;
}

function halfNumber(num){

    num = prompt("Enter a number to be halved");
    var result = num/2;
    var txt = document.getElementById('halved_answer');
    txt.innerHTML = "Half of " + num + " is " + result;
    return result;

}

function crazyFunction(num){
    result1 = squarenumber(num);
    result2 = halfNumber(result1);

    return result2;
}

HTML

<ol>
    <li><a href="#" id="squarenum">squarenumber</a></li>

    <p id='square_answer'>The square is...</p>
    <li><a href="#" id='halfnum'>halfNumber</a></li>
    <p id='halved_answer'>Half of the number is...</p>
    <li><a href="#" id='crazyfunction'>crazyfunction</a></li>
    <p id='crazy_answer'>This function is a surprise...</p>
</ol>

There are a few ways to solve your issue. Firstly, try understand why you're creating these functions and understand the requirements.

The expected output is that when squarenumber or halfNumber are called individually, you want a prompt to show, otherwise if they are called from within crazyFunction , the desired output is to not show a prompt.

So, we can see that the fluctuation in control flow lies at the moment of the function being called. Therefore, it would make the most sense for you to come up with a solution to this problem based on the passed in arguments to the sub functions of squarenumber and halfNumber .

Basic Solution

function crazyFunction(num) {
    boolean shouldShowPrompt = false;
    let result1 = squarenumber(num, shouldShowPrompt);
    let result2 = halfNumber(result1, shouldShowPrompt);

    return result2;
}

And then the halfNumber example:

function halfNumber(num, shouldShowPrompt ) {

    if (shouldShowPrompt) {
        num = prompt("Enter a number to be halved");
    }

    var result = num / 2;
    var txt = document.getElementById('halved_answer');
    txt.innerHTML = "Half of " + num + " is " + result;
    return result;

}

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM