问题描述:

I have been stuck on this homework:

Create a generic function that outputs one line of the countdown on

the web page, followed by an alert, and receives the data to output as

an input parameter.

Use that function to output each line of the countdown, and an alert.

Please note that you are outputting the countdown to the browser

window this time, not to an alert!

The alert is only being used to signal when to output the next line

I need help in how to come up with a generic function that passes only one argument and then can be called 13 times. To write a for loop that output the numeric part of a countdown.

网友答案:

I think the key here is that they're asking for "Generic".

That means one function that doesn't have to know anything but what it's doing.
It also usually means that it shouldn't remember anything about what it did last time, or what it's going to do next time it's called (unless you're writing a generic structure specifically for remembering).

Now, the wording of the specification is poor, but a generic function which:

  1. takes (input) data to write
  2. writes the input to the page
  3. calls an alert

is much simpler than you might think.

var writeInputAndAlert = function (input) {
    // never, ever, ***ever*** use .write / .writeLn in the real world
    document.writeLn(input);
    window.alert("next");
};

If I was your teacher, I would then rewrite window.alert to handle the non-generic portion.
It's non-generic, because it knows the rules of the program, and it remembers where you are, and where you're going.

var countFrom    = 100,
    currentCount = countFrom,
    countTo      = 0;

var nextCount = function () {
    currentCount -= 1;
    if (currentCount >= countTo) { writeInputAndAlert(currentCount); }
};

window.alert = nextCount;

edit

var countdownArray = ["ten", "nine", "eight", "Ignition Start", "Lift Off", "We have Lift Off"],
    i = 0, end = countdownArray.length, text = "",

    printAndAlert = function (item) {
        alert();
        document.write(item);
    };

for (; i < end; i += 1) {
    text = countdownArray[i];
    printAndAlert(text);
}

This really doesn't need to be any harder than that.
printAndAlert is a generic function that takes one input, writes that input and triggers an alert.
You call it inside of a for loop, with each value in your array.
That's all there is to it.

网友答案:

If I understand correctly, you want to create a function that will allow you to pass the data once, and then you can call that function to output the data line by line.

To do this exactly that way isn't possible, but this method is almost the same:

function createOutputFunction(dataArray)
{
    return function() {
        document.write(dataArray.shift()); // This writes the first element of the dataArray to the browser
    };
}

//It can then be used like this
outputFunction = createOutputFunction(["Banana", "Mango", "Apple"]);

outputFunction();
outputFunction();
outputFunction();

The "createOutputFunction" function returns a function that can read the "dataArray" variable and print its first element every time it is called.

相关阅读:
Top