Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   How to add new items in my script? (http://www.programmingforums.org/showthread.php?t=12337)

cabdirazzaq Jan 9th, 2007 2:24 PM

How to add new items in my script?
 
Let's see if you can help me manage this one... I have this script to help one memorize things. The thing is, I want the user to be able to add their own items/words into the list (not me doing it before hand). Here is the script:
Quote:

<html>
<head>
<title>Memory tool v.1.0</title>
<script language="Javascript">
var Dictionary = new Array;
var WordsPerPass = 0;
var CurrentWords = '';
function Populate()
{
Dictionary[0] = "Cat";
Dictionary[1] = "Dog";
Dictionary[2] = "Mouse";
Dictionary[3] = "Platypus";
Dictionary[4] = "C";
Dictionary[5] = "Programming";
Dictionary[6] = "Is";
Dictionary[7] = "Fun";
Dictionary[8] = "Yes";
WordsPerPass = 4;
ResetForm();
}
function FillWords()
{
if(CurrentWords != '')
return;
var WordCount = 0;
while(true)
{
var Rand = Math.random();
Rand = Math.round(Rand * (Dictionary.length - 1));
if(CurrentWords.search(Dictionary[Rand]) != -1)
continue;
else
{
CurrentWords += Dictionary[Rand] + ' ';
WordCount++;
if(WordCount == WordsPerPass)
break;
}
}
document.getElementById("Words").value = CurrentWords.replace(/ /g, '\n');
document.getElementById("StartButton").disabled = true;
setTimeout("EraseWords()", document.getElementById("TimeInMinutes").value * 100000);
}
function EraseWords()
{
document.getElementById("Words").value = '';
document.getElementById("StartButton").style.display = 'none';
document.getElementById("StopButton").style.display = 'inline';
}
function CompareWords()
{
var Answer = document.getElementById("Words").value;
var WordsCheckedCount = 0;
var Response = '';
while(true)
{
var Word = Answer.match(/\b.+?\b/);
if(Word == null)
break;
if(CurrentWords.match(Word) == null)
{
Response += 'Incorrect: '+Word+'\n';
}
else
{
Response += 'Correct: '+Word+'\n';
}
Answer = Answer.replace(Word, '');
WordsCheckedCount++;
if(WordsCheckedCount == WordsPerPass)
break;
}
document.getElementById("Words").value = Response + '\n\nAnswers: \n'+CurrentWords.replace(/ /g, '\n');
document.getElementById("StopButton").style.display = 'none';
document.getElementById("ResetButton").style.display = 'inline';
}
function ResetForm()
{
document.getElementById("Words").value = 'Click Start To Begin';
document.getElementById("StartButton").style.display = 'inline';
document.getElementById("StartButton").disabled = false;
document.getElementById("ResetButton").style.display = 'none';
CurrentWords = '';
WordCount = 0;
}
</script>
</head>
<body onLoad="Populate()">
<textarea cols="40" rows="20" id="Words"></textarea>
<select id="TimeInMinutes">
<option value="0.01"> Short</option>
<option value="0.5"> 30 Seconds</option>

<option value="1"> 1 Minute</option>
<option value="2"> 2 Minutes</option>
<option value="3"> 3 Minutes</option>
</select>
<input style="display:inline" type="button" id="StartButton" value="Start" OnClick="FillWords()"/>
<input style="display:none" type="button" id="StopButton" value="Compare", onClick="CompareWords()"/>
<input style="display:none" type="button" id="ResetButton" value="Start Over" onClick="ResetForm()"/>
</body>
</html>
Please, if you could because I really need some help on this one :o

Arevos Jan 9th, 2007 4:20 PM

It would help if you used the [code] tags. Then we could see your indentation. Also, why have you multiplied by 100000 to get minutes? It should be 60000.

As for populating your word list, why not have the user enter in some words, then do something like:
:

dictionary = document.getElementById("userwords").value.split(" ");
Also, I'd advise getting the Prototype library. It really makes Javascript coding a heck of a lot easier.


All times are GMT -5. The time now is 1:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC