473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Simple JavaScript question

Please excuse this very basic question; I'm just starting to learn
JavaScript.

I have just modified added a toUpperCase statement to change strings
to upper case, but it does not run.

Can you please tell me what I have done wrong? I have pasted the code
in below.

Thank you.

-------------------------------------------------------------------------------------------------

// JavaScript Document

// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
SortNames toUpperCase();
//Change the names to all upper case
thename=document.theform.newname.value;
// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;
// Sort the array
names.sort();
document.theform.sorted.value=names.join("\n");

Feb 17 '07 #1
4 1478
pa************@gmail.com wrote on 17 feb 2007 in comp.lang.javascript:
// JavaScript Document

// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
SortNames toUpperCase();
The name of a function cannot return a string in itself,
[unless you programme recursive, which is not here]

toUpperCase() should be attached by a period like this:

var a = 'A StrinG';
alert( a.toUpperCase(); ); // shows: A STRING

//Change the names to all upper case
thename=document.theform.newname.value;
This could work, if the form is correctly named

better keep your variables local, and do this:

var thename =
document.forms['theform'].elements['newname'].value;

// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;
You forgot to define a loop, this way only names[0]
is created and assigned
// Sort the array
names.sort();
document.theform.sorted.value=names.join("\n");
you will need to end the function with a }

============

If, Paul, I did not misread your intended purpose,
perhaps the tested code below will help you:

============= test.html ======================
<script type='text/javascript'>

function SortNames() {
var names = new Array();
var f = document.forms['theForm'];
for (var n=0;n<3;n++)
names[n] =
f.elements['elem' + n].value.toUpperCase();
f.elements['sorted'].value =
names.sort().join("+");
return false;
};

</script>
<form name='theForm' onsubmit='return SortNames();'>
<input name='elem0' value='xyz x'><br>
<input name='elem1' value='ABC x'><br>
<input name='elem2' value='Pqr x'><br>
<input type='' name='sorted' value=''><br>
<input type='submit' value='Sort me'><br>
</form>
=================================================

Are you sure I am not making your school assignment?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 17 '07 #2
On Feb 17, 11:28 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
paul.denlin...@gmail.com wrote on 17 feb 2007 in comp.lang.javascript:
// JavaScript Document
// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
SortNames toUpperCase();

The name of a function cannot return a string in itself,
[unless you programme recursive, which is not here]

toUpperCase() should be attached by a period like this:

var a = 'A StrinG';
alert( a.toUpperCase(); ); // shows: A STRING
//Change the names to all upper case
thename=document.theform.newname.value;

This could work, if the form is correctly named

better keep your variables local, and do this:

var thename =
document.forms['theform'].elements['newname'].value;
// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;

You forgot to define a loop, this way only names[0]
is created and assigned
// Sort the array
names.sort();
document.theform.sorted.value=names.join("\n");

you will need to end the function with a }

============

If, Paul, I did not misread your intended purpose,
perhaps the tested code below will help you:

============= test.html ======================
<script type='text/javascript'>

function SortNames() {
var names = new Array();
var f = document.forms['theForm'];
for (var n=0;n<3;n++)
names[n] =
f.elements['elem' + n].value.toUpperCase();
f.elements['sorted'].value =
names.sort().join("+");
return false;

};

</script>

<form name='theForm' onsubmit='return SortNames();'>
<input name='elem0' value='xyz x'><br>
<input name='elem1' value='ABC x'><br>
<input name='elem2' value='Pqr x'><br>
<input type='' name='sorted' value=''><br>
<input type='submit' value='Sort me'><br>
</form>
=================================================

Are you sure I am not making your school assignment?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sorry I did not give you the URL for the HTML page; it's
http://www.china-ready.com/jstest/arraysorting.htm Hope this makes
what I'm trying to do clearer.

Actually, no I'm not doing a school assignment; this is entirely self-
imposed. I'm working my way thru Sams Teach Yourself Javascript in 24
Hours. This is the exercise at the end of Hour 5.

If you have any suggestions about how to use Firebug to debug
JavaScript; I'd appreciate your input. I'm so new that I can't figure
out the debug messages.

Thanks again!

Paul

Feb 17 '07 #3
pa************@gmail.com wrote on 17 feb 2007 in comp.lang.javascript:
>If, Paul, I did not misread your intended purpose,
perhaps the tested code below will help you:

============= test.html ======================
<script type='text/javascript'>

function SortNames() {
var names = new Array();
var f = document.forms['theForm'];
for (var n=0;n<3;n++)
names[n] =
f.elements['elem' + n].value.toUpperCase();
f.elements['sorted'].value =
names.sort().join("+");
return false;

};

</script>

<form name='theForm' onsubmit='return SortNames();'>
<input name='elem0' value='xyz x'><br>
<input name='elem1' value='ABC x'><br>
<input name='elem2' value='Pqr x'><br>
<input type='' name='sorted' value=''><br>
<input type='submit' value='Sort me'><br>
</form>
=============================================== ==

Are you sure I am not making your school assignment?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Sorry I did not give you the URL for the HTML page; it's
http://www.china-ready.com/jstest/arraysorting.htm Hope this makes
what I'm trying to do clearer.
Trying your page, IE shows javascript errors, that you can read and see
the line number. Debugging those errors is your first line of defence.
Actually, no I'm not doing a school assignment; this is entirely self-
imposed. I'm working my way thru Sams Teach Yourself Javascript in 24
Hours. This is the exercise at the end of Hour 5.
It seems you should try to understand the code, thoroughly bedore jumping
to the next. I never used a book for learning JS, as there is so much
code on the web that you can view source and try out.
If you have any suggestions about how to use Firebug to debug
Never heard of that, is that the standard FF debugger?
JavaScript; I'd appreciate your input. I'm so new that I can't figure
out the debug messages.
Debugging in the teaching fase should be done by hand, using small
snippets of code at a time [modular programming] that you test so tthat
you are absolutely sure it works as planned.

Put in alert(aVariable); bedore and after the error line to inspect
errors and eliminate them one by one.

IE shows nice error warnings. FF too I am told.

[in IE you should switch "friendly errors" off, they are vey unfriendly
while debugging]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 17 '07 #4
pa************@gmail.com wrote:
>
If you have any suggestions about how to use Firebug to debug
JavaScript; I'd appreciate your input. I'm so new that I can't figure
out the debug messages.
Just use the JavaScript console and you will see:

missing ; before statementsort.js (line 8)
SortNames toUpperCase()(

The typo is pretty obvious once it has been pointed out!

--
Ian Collins.
Feb 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: not | last post by:
Hello All, I am trying to develop a relatively simple self-quiz form in javascript, but I'm having no luck getting it to work. What I am looking for is a script that allow the user to select...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
1
by: number1.email | last post by:
Hello, I have a simple Web Page Questionairre in which questions are read from a database, and the user can indicate the correct answer via either a radio input control or a dropdown list. The...
3
by: Shelly | last post by:
Hi, I really don't know javascript, though I can understand existing code. I know java, php, etc. Here is my question, and I assume it is simple. I have a page. On this page I have element_1...
6
by: alex.kemsley | last post by:
Hi guys, I am trying to write a simple script in vain. I need it to first check to see if the cookie exists then if not write one with two variable var ref = document.referrer var page =...
4
by: Brian Piotrowski | last post by:
Hi All, After many years of ASP 3.0, I am finally taking the plunge and moving to ASP.NET. I'm going through a few tutorials, and I'm wondering if someone can answer me a question on adding a...
13
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python...
7
by: James Harris | last post by:
My Googling has let me down here - maybe because when it comes to Java I don't really know which type I need! If someone could point me in the right direction I'd appreciate it. My requirement...
5
by: fussfart | last post by:
I'm trying to do something that should be very simple but isn't working! (I also want to do something somewhat more complicated, but that has to wait until I figure out the simple stuff.) First, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.