-2

Create a generic function that outputs one line of the countdown (ten, nine, eight, seven etc.) 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.

I need to output the countdown to the browser window and an alert is only being used to signal when to output the next line.

I need to use a loop. I also can't use an array.

This is what I have so far.
But I can’t figure out how to define the function that has an input parameter that contains the line to output.

for (var count=10; count >= 1; count--) { 
    window.alert("Click Okay!"); 
    document.write(count+"<br />") 
}
{ 
    window.alert("Click Okay!") 
    document.write("Ignition Start<br />") 
    window.alert("Click Okay!") 
    document.write("Liftoff<br />") 
    window.alert("Click Okay!") 
    document.write("We Have Liftoff!<br />") 
} 
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
JSbeginnger
  • 11
  • 1
  • 2
  • Java is not JavaScript. – markspace Sep 08 '18 at 18:49
  • 2
    Put your code in the question, not in the comment area. And mention the problem in the title without saying as just a question. – Dean Johns Sep 08 '18 at 18:49
  • 1
    Also, please post any updates in your question by editing it, not in comments. Especially code as the comment formats code into an unreadable mess. – markspace Sep 08 '18 at 18:49
  • I took liberty to include your code from the comment in your question, because that is where it should go. Please fix obvious syntax problems like missing semicolons after statements, and I'm not clear about that braced section after the loop, this is probably meant to be part of the loop? then we can look further. – Cee McSharpface Sep 08 '18 at 19:14
  • Hi! thanks. can you look at what Andre adding below and my comments to his code? – JSbeginnger Sep 08 '18 at 19:25
  • maybe an artefact of the browser. the sequence of events is correct in his code according to your description. you may need to [use this approach](https://stackoverflow.com/a/13820548/1132334) on any modern browser. – Cee McSharpface Sep 08 '18 at 19:41
  • the entire countdown shows up on the webpage all at once after i click the alert 10 times in a row, which isn't what i need it to do. i need to click alert and have the count down write to the web page one at a time only after i click the ok alert – JSbeginnger Sep 08 '18 at 19:53
  • I understood as much. read my previous comment, it includes a possible reason for the behavior you observe, and a link to a possible solution. – Cee McSharpface Sep 08 '18 at 20:01

1 Answers1

0
<html>
<body>
</body>

<script type="text/javascript">
var t = 1;
function show(text) {
  window.setTimeout(()=>{
        var n = document.createElement("div");
        n.innerHTML = text;
        document.body.appendChild(n);
    window.alert("Click Okay!");
        //document.body.style.display = "none";
        //document.body.style.display = "";
    },t);
    t+=20;
}

for (var count=10; count >= 1; count--) { 
    show(count.toString());
} 
show("Ignition Start");
show("Liftoff");
show("We Have Liftoff!");
</script>
</html>
Andrei Bozantan
  • 3,781
  • 2
  • 30
  • 40
  • thanks. so this will write each number to my page one by one only after i click the 'click Okay' alert? when it gets down to 1, i need it to then also write to the web page Ignition Start, Liftoff, We have Liftoff! the same way i did the countdown. can you please help me with this ? – JSbeginnger Sep 08 '18 at 19:12
  • i tested this Andrei, and it's almost right. right now its having me click the click okay alert 10 times and then it writes the numbers to the web page. i need it to write a number to the web page after each click of the click okay alert – JSbeginnger Sep 08 '18 at 19:14
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Sep 08 '18 at 19:41
  • the entire countdown shows up on the webpage all at once after i click the alert 10 times in a row, which isn't what i need it to do. i need to click alert and have the count down write to the web page one at a time only after i click the ok alert – JSbeginnger Sep 08 '18 at 19:56
  • I made some changes in the script, now it should work like you want. – Andrei Bozantan Sep 08 '18 at 20:44
  • i think this uses some stuff we haven't learned about. "The purpose of the assignment is to demonstrate your ability to use operators and flow control logic, and to create a function which accepts an input parameter, and uses the data passed to the function." – JSbeginnger Sep 10 '18 at 15:10