![]() |
|
|
#1 |
|
Eligible for custom title
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
|
JavaScript
Hey everyone, just getting started in JavaScript, and basically it is kicking my butt so far. It seems that the class is more for an intermediate learner than a beginner
![]() None the less, I have this assignment due, and many of us students are completely in the dark here. LOL Anyhow, this is the question we must answer. Write JavaScript code that anticipates and handles an error for an expected numeric field. This code is executed on keypress and the entered value is saved for you in a variable called enteredChar. Include the try block of JavaScript statements needed to check if the character is not a number or a non-alphanumeric character or if you throw an error message. Any help would be much appreciated
__________________
#3 Forever A Fan! Just Because I Don't Care Doesn't Mean I Don't Understand Check our team Status on Free-DC |
|
|
|
|
|
#2 |
![]() Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts
|
Unfortunately my disturbingly large database of previously written code is offline right now so I'm doing this off google.
Code:
try {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line = stdin.readLine();
int enteredChar = Integer.parseInt(line);
}
catch (IOException e) { System.out.println(e); }
catch (NumberFormatException e) { System.out.println(e); }
I can't tell if the instructions are unclear or I'm just an idiot but you may want the variable "line" to be "enteredChar" and vice versa.
__________________
Last edited by streetfighter 2; Oct 28, 2010 at 03:39 AM. |
|
|
|
| The Following User Says Thank You to streetfighter 2 For This Useful Post: |
|
|
#3 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,577 (6.28/day)
Thanks: 1,755
Thanked 2,596 Times in 1,960 Posts
|
That looks like Java, not JavaScript.
You'll want the parseInt (Ints do not have any values after the decimal point) or parseFloat (Floats may or may not have data after the decimal point) command: http://www.javascripter.net/faq/convert2.htm If they are invalid, it will return 0 or NaN (Not a Number) depending on browser. Pseudo-code: Code:
var converted = parseInt(USERINPUT);
if ((converted == 0) || (converted == "NaN")) {
// Return error message
} else {
// Proceed with code
}
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following 2 Users Say Thank You to FordGT90Concept For This Useful Post: |
|
|
#4 |
![]() Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts
|
Indeed it is Java. I didn't even notice I was supposed to write in "JavaScript". My bad.
The code you wrote looks good to me. If you want a try-catch based script you'll need something like this: Code:
var x=prompt("Enter a number between 0 and 10:","");
try
{
if(x>10)
{
throw "Err1";
}
else if(x<0)
{
throw "Err2";
}
else if(isNaN(x))
{
throw "Err3";
}
}
catch(er)
{
if(er=="Err1")
{
alert("Error! The value is too high");
}
if(er=="Err2")
{
alert("Error! The value is too low");
}
if(er=="Err3")
{
alert("Error! The value is not a number");
}
}
__________________
|
|
|
|
| The Following User Says Thank You to streetfighter 2 For This Useful Post: |
|
|
#5 |
|
Eligible for custom title
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
|
Hmm, from what I have been reading, and this example that I am posting, I am not understanding what your doing here.....sorry for my ignorance
![]() Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>onerror Event Handler</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
function processErrors(errMessage, errURL, errLineNum) {
window.alert("Error: " + errMessage + "\n"
+ "File: " + errURL + "\n"
+ "Line: " + errLineNum);
return true;
}
window.onerror=processErrors;
/* ]]> */
</script>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
document.wrte("My name is Don.");
/* ]]> */
</script>
</body>
</html>
I guess I am just confused on how this would be re-wrote to my class question LOL.
__________________
#3 Forever A Fan! Just Because I Don't Care Doesn't Mean I Don't Understand Check our team Status on Free-DC |
|
|
|
|
|
#6 | |
|
Eligible for custom title
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
|
Quote:
![]() ![]() ![]() ![]() This code makes much more sense LOL. Wow guys, thanks for the ultra quick responses!!!
__________________
#3 Forever A Fan! Just Because I Don't Care Doesn't Mean I Don't Understand Check our team Status on Free-DC |
|
|
|
|
|
|
#7 |
![]() Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts
|
I write code in so many different languages that I often get confused about which one is which. That and my memory is going...
As languages go JavaScript is one of the easier ones to learn because of it's syntactic simplicity. Though it's not quite as fundamental as brainfuck.
__________________
|
|
|
|
| The Following User Says Thank You to streetfighter 2 For This Useful Post: |
|
|
#8 | |
|
Eligible for custom title
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
|
Quote:
__________________
#3 Forever A Fan! Just Because I Don't Care Doesn't Mean I Don't Understand Check our team Status on Free-DC |
|
|
|
|
|
|
#9 | |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,577 (6.28/day)
Thanks: 1,755
Thanked 2,596 Times in 1,960 Posts
|
Quote:
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#10 |
|
Eligible for custom title
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
|
Thanks a ton guys! Of course I am still somewhat confused, but then again, that's not unusual
![]() ![]() ![]()
__________________
#3 Forever A Fan! Just Because I Don't Care Doesn't Mean I Don't Understand Check our team Status on Free-DC |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Javascript Hw Problem | epicfail | Programming & Webmastering | 4 | May 12, 2010 05:04 PM |
| Need javascript Help please | wolf2009 | Programming & Webmastering | 1 | Nov 30, 2009 11:06 PM |
| which Eclipse version for Javascript? | Braveheart | Programming & Webmastering | 1 | Oct 20, 2009 04:03 PM |
| Javascript return doesn't work? | aximbigfan | Programming & Webmastering | 0 | Mar 30, 2008 12:53 AM |
| JavaScript opens doors to browser-based attacks | Alec§taar | General Software | 4 | Jul 31, 2006 02:50 PM |