Java Script help (1 Viewer)

tl mike

Registered User.
Local time
Today, 07:21
Joined
Sep 7, 2007
Messages
117
I have been working on a javascript application for my class that I am taking online and I am completely stumped the book we are using doesnt have the best formate in helping teach how javascript works (probably becasue trying to put tomuch into a small book)

My Problem is when the user finally types exit after entering thier fav character multiple times it is supposed to show from the user array the characters they had entered.

Any help would be greatly appreciated

My code is:
Code:
<html>
<head>
<title>Fav Character</title>
<script language= "JavaScript" type="text/javascript">
/*****************************************
File: Assignement3_LOR.html
Author: Michael Holt
Assignment: 3
Create Date: 02/21/2009
Last Modified: 02/25/2009
****************************************/
/****************************************
PROGRAM FUNCTION
- Setup an array with characters names to compare with users character
- Ask user to enter favorite character
 + save user favorite character info
 + loop the question so the user is asked until enter "exit"
- Once enter "exit" document write
 + favorite characters entered in order
 + date in which the user performed such task
***************************************/
// get date ***********************
var dateNow = new Date ();
 var dateMonth = dateNow.getMonth ();
  var myMonth = ++dateMonth;
 var myDay = dateNow.getDate ();
var myYear = dateNow.getFullYear ();
// get date end********************
// question function
function askMe()
{
// start favorite_Character array
  var user_array = new Array ();
  var counter = 0;
  var my_list = user_array.join("<br />");
 
 do
  {
  var user_answer = prompt("Who's your favorite character?\n\r\(Enter 'exit' to end prompting\)","");
  user_array[counter] = user_answer;
  counter++;
  }
  while(user_answer!== "exit");

 document.write("<p><b>Todays Date:</b>"+myMonth+"/"+ myDay +"/"+myYear+"<br><br></p>");
  document.write("<p>Press the Reload/ Refresh Button to ask again</p>");
   document.write("You entered" +my_list);
}
</script>
</head>
<body>
<script>
 askMe();
</script>
</body>
</html>
 

richard05

Registered User.
Local time
Today, 15:21
Joined
Mar 18, 2009
Messages
10
Here is a similar working piece of code of what you are looking it to do

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "[URL]http://www.w3.org/TR/html4/loose.dtd[/URL]">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Store prompt input in array</title>
<style type="text/css">
<!--
body{
font-family:Verdana, Helvetica;
font-size:12px;
color:#000;
background-color:#f8f8ff;
}
.main{
width:100%;
text-align:center;
padding-top:100px;
}
-->
</style>
<script language="javascript" type="text/javascript">
<!--
var givenNames = new Array();
// this pattern matches to words and/or digits up from 1 char
// anything else is ignored
var pattern = /[\w\d]{1,}/ig;
do{
var name = prompt("Enter some names. Only letters and digits are accepted!\nEntering an empty field stops asking","");
if(name && name.match(pattern)){givenNames.push(name);}
}
while(name != "");
function displayNames(){
if(givenNames.length > 0){
document.getElementById("list").innerHTML = "<span style='color:Navy;font-weight:bold;'>Given names are:<\/span><br><br>" + givenNames.join("<br><br>");
}
else{document.getElementById("list").innerHTML = "<span style='color:Navy;font-weight:bold;'>Nothing has been given!<\/span>";}
}
//-->
</script>
</head>
<body onload="setTimeout('displayNames()',500)">
<center><h2>Store prompt input in array, then display it</h2></center>
<div id="list" class="main"></div>
</body>
</html>
 

Users who are viewing this thread

Top Bottom