/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Need some help with this script

Hey guys, I need some help with the following script:

[QUOTE]

<html>
<head>
<title></title>
</head>

<body>
<script type=”text/javascript”>
var a = new Array();
function list()
{ var s = “”;
for(i = 0; i < a.length; i++)
s = s + a[i] + “n”;
alert(s); }

function add()
{ var myTextField = document.getElementById(“myText”);

if (document.getElementById(“myText”).value.length == 0)
{ alert(“Enter a Name”);
return false; }

a[a.length] = myTextField.value;
myTextField.value = “”;
return true;
}

function find()
{var myTextField = document.getElementById(“myText”);

if (document.getElementById(“myText”).value.length == 0)
{ alert(“Enter a Name”);
return false; }
else if (document.getElementById(“myText”).value != a)
{ alert(“Your Name not found”);
return false; }
else if (document.getElementById(“myText”).value == a)
{ alert(“Your Name found”);
return true; }
}
</script>
<input type=”text” id=”myText” /><br>
<input type=”button” onclick=”add()” value=”Add a name” />
<input type=”button” onclick=”list()” value=”List the names” />
<input type=”button” onclick=”find()” value=”Find” />
</body>
</html>

[/QUOTE]

Everything works but I have the following bug.
When only one name is added to the array, the find function works great.
When more than one names are added to the array the find function brakes…

Any ideas or help in order to understand and solve the problem will be very appreciated.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@twocoldNov 10.2009 — You cannot use the string charactors to compare with the array object as "document.getElementById("myText").value == a".


Try the codes as bellows,hope it can help you.

<script type="text/javascript">

var a = new Array();

function list()

{

var s = "";

for(i = 0; i < a.length; i++)

s = s + a[i] + "n";

alert(s);

}



function add()

{

var myTextFieldValue = document.getElementById("myText").value;

if (!myTextFieldValue)

{

alert("Enter a Name");

return false;

}



a[a.length] = myTextFieldValue;

return true;

}

function find()

{

var myTextFieldValue = document.getElementById("myText").value;

if (!myTextFieldValue)
{
alert("Enter a Name");
return false;
}
else
{
var strNames = a.join(",");
alert(strNames);
if(strNames.indexOf(myTextFieldValue)!=-1){
alert("Your Name is found");
return true;
}
else{
alert("Your Name not found");
return false;
}
}

}

</script>
Copy linkTweet thisAlerts:
@JMRKERNov 10.2009 — Another (similar) version.

Your problem was that you were not checking the elements of the 'a' array.

Note that neither version checks for:

1. Duplicate name entries.

2. Upper or lower case entries

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Look For Name&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1046952#post1046952

var a = new Array();
function list() {
var s = "";
for(i = 0; i &lt; a.length; i++)
s = s + a[i] + "n";
alert(s);
}

function add() {
var myTextField = document.getElementById("myText");
if (document.getElementById("myText").value.length == 0) { alert("Enter a Name"); return false; }
a[a.length] = myTextField.value;
myTextField.value = "";
return true;
}

function find() {
var myTextField = document.getElementById("myText");
if (document.getElementById("myText").value.length == 0) { alert("Enter a Name"); return false; }
var fnd = -1;
for (var i=0; i&lt;a.length; i++) {
if (document.getElementById("myText").value == a[i]) {
alert("Your Name found");
return i;
}
}
alert("Your Name not found");
return fnd;
}
&lt;/script&gt;
&lt;input type="text" id="myText" /&gt;&lt;br&gt;
&lt;input type="button" onclick="add()" value="Add a name" /&gt;
&lt;input type="button" onclick="list()" value="List the names" /&gt;
&lt;input type="button" onclick="find()" value="Find" /&gt;
&lt;/body&gt;
&lt;/html&gt;


Good Luck!

?
Copy linkTweet thisAlerts:
@nMIK-3authorNov 10.2009 — [B]twocold[/B]

Thanks so much for your reply.

Is there any way of debugging the original code without the varstrNames you declare?

I am looking if possible to only modify the :

"document.getElementById("myText").value == a"

and

"document.getElementById("myText").value != a"

in order to make them compatible with the multiple names search.

I tried putting i and i++ in the above code with no luck...

Any ideas?

thanks again
Copy linkTweet thisAlerts:
@nMIK-3authorNov 10.2009 — Another (similar) version.

Your problem was that you were not checking the elements of the 'a' array.

Note that neither version checks for:

1. Duplicate name entries.

2. Upper or lower case entries

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Look For Name&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1046952#post1046952

var a = new Array();
function list() {
var s = "";
for(i = 0; i &lt; a.length; i++)
s = s + a[i] + "n";
alert(s);
}

function add() {
var myTextField = document.getElementById("myText");
if (document.getElementById("myText").value.length == 0) { alert("Enter a Name"); return false; }
a[a.length] = myTextField.value;
myTextField.value = "";
return true;
}

function find() {
var myTextField = document.getElementById("myText");
if (document.getElementById("myText").value.length == 0) { alert("Enter a Name"); return false; }
var fnd = -1;
for (var i=0; i&lt;a.length; i++) {
if (document.getElementById("myText").value == a[i]) {
alert("Your Name found");
return i;
}
}
alert("Your Name not found");
return fnd;
}
&lt;/script&gt;
&lt;input type="text" id="myText" /&gt;&lt;br&gt;
&lt;input type="button" onclick="add()" value="Add a name" /&gt;
&lt;input type="button" onclick="list()" value="List the names" /&gt;
&lt;input type="button" onclick="find()" value="Find" /&gt;
&lt;/body&gt;
&lt;/html&gt;


Good Luck!

?[/QUOTE]


This is exactly the way I wanted to solve it. Is always the small stuff I do not pay attention to.. ?

Thank you very much for spotting out the error and helping me with the debugging..

Problem solved!

Thanks again both of you.
Copy linkTweet thisAlerts:
@twocoldNov 10.2009 — For JMRKER information,and your need, I'm changing the codes...

var length = a.length;

var find = -1;

while(length >0){

var currentValue = document.getElementById("myText").value.toLowerCase();

var arrayValue = a[--length].toLowerCase();

if( currentValue == arrayValue){

alert("Your Name found");

find = length;

break;

}

}

if(find == -1){

alert("Your Name not found");

return -1;

}
Copy linkTweet thisAlerts:
@nMIK-3authorNov 10.2009 — @twocold thank for the additional info mate

@JMRKER one question regarding your code.

What exactly is the point of [B]var fnd[/B] you put??

I am asking that because even [B]without [/B]the [B]var fnd = -1[/B] and without the [B]return fnd;[/B] in the end, everything works OK as well.

So what is the point of those? Not that it matters, but I want to completely understand the concept.
Copy linkTweet thisAlerts:
@JMRKERNov 10.2009 — 
@JMRKER one question regarding your code.

What exactly is the point of [B]var fnd[/B] you put??

I am asking that because even [B]without [/B]the [B]var fnd = -1[/B] and without the [B]return fnd;[/B] in the end, everything works OK as well.

So what is the point of those? Not that it matters, but I want to completely understand the concept.[/QUOTE]


Initially I was using ''fnd' for debugging purposes with the thought that you might want to know WHICH element of the 'a' array contained the matching name to the input. I could return the location within the 'a' array if found and -1 if not. Your code did not need that function, so yes, it is safe to remove the 'fnd' logic because if it is not used then it is not needed.

I'm glad I could help and I believe the other members are just as happy to help.

Good Luck!

?
Copy linkTweet thisAlerts:
@nMIK-3authorNov 10.2009 — Initially I was using ''fnd' for debugging purposes with the thought that you might want to know WHICH element of the 'a' array contained the matching name to the input. I could return the location within the 'a' array if found and -1 if not. Your code did not need that function, so yes, it is safe to remove the 'fnd' logic because if it is not used then it is not needed.

I'm glad I could help and I believe the other members are just as happy to help.

Good Luck!

?[/QUOTE]

Thanks for the clarification, I really appreciate it!
Copy linkTweet thisAlerts:
@JMRKERNov 10.2009 — For JMRKER information,and your need, I'm changing the codes...

var length = a.length;

var find = -1;

while(length >0){

var currentValue = document.getElementById("myText").value.toLowerCase();

var arrayValue = a[--length].toLowerCase();

if( currentValue == arrayValue){

alert("Your Name found");

find = length;

break;

}

}

if(find == -1){

alert("Your Name not found");

return -1;

}[/QUOTE]


That takes care of the upper/lower case checks. ?

I was also pointing out that the 'add' function might be better served to check to see if the same name had already been entered, hence the 'duplicate entry' check. The corrected 'find' function would find the first of any duplicated entries like 'jmrker' and 'JMRKER' and "JmRkEr" and etc.
Copy linkTweet thisAlerts:
@twocoldNov 10.2009 — hmm,your suggestion is helpful for me too.thanks!
Copy linkTweet thisAlerts:
@JMRKERNov 10.2009 — For both 'nMIK-3' and 'twocold', even though problem already RESOLVED.

Here are the changes I suggested put into your code.

Note:

1. I also added the 'fnd' logic back into 'find()' function to show my original intent.

2. Added a sorted display of names entered and shared function with other functions.

3. Put in upper/lower case checks as previously suggested.

4. Compressed code a bit with use of variable to represent 'document.getElementById()' logic (see add() and find() functions)

5. Shortened list() function a bit

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Look For Name&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1046952#post1046952

var a = new Array();
function list() {
if ((a.length-1) &lt; 0) { return 'No names entered yet'; }
return a.sort().join('n'); // sort list prior to display
}

function add() {
var myTextField = document.getElementById("myText");
var str = '';
if (myTextField.value.length == 0) { str = "Enter a Name"; return str; }
var fnd = find();
if (fnd &lt; 0) {
a[a.length] = myTextField.value;
myTextField.value = "";
str = '';
} else { str = 'Name already enterednn'+list(); }
return str; <br/>
}

function find() {
var myTextField = document.getElementById("myText");
var fnd = -1;
if (myTextField.value.length == 0) { alert("Enter a Name"); return fnd; }
for (var i=0; i&lt;a.length; i++) {
if (myTextField.value.toLowerCase() == a[i].toLowerCase()) { // alert("Your Name found");
return i;
}
} // alert("Your Name not found");
return fnd;
}
&lt;/script&gt;
&lt;input type="text" id="myText" /&gt;&lt;br&gt;
&lt;input type="button" onclick="var str=add();if(str!=''){alert(str)}" value="Add a name" /&gt;
&lt;input type="button" onclick="alert(list())" value="List the names" /&gt;
&lt;input type="button"
onclick="var fnd=find();if(fnd&lt;0){alert('Your name NOT found')}else{alert('Your name found')}"
value="Find" /&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @nMIK-3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...