# JavaScript



## stinger608 (Oct 28, 2010)

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


----------



## streetfighter 2 (Oct 28, 2010)

Unfortunately my disturbingly large database of previously written code is offline right now so I'm doing this off google.


```
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); }
```

What it does is grabs a line of characters (numbers/letters/etc) from the standard input and then attempts to parse it as an integer.  If the input is not an integer it throws a "NumberFormatException", all other errors are related to IO and throw an "IOException".

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.


----------



## FordGT90Concept (Oct 28, 2010)

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:


```
var converted = parseInt(USERINPUT);
if ((converted == 0) || (converted == "NaN")) {
  // Return error message
} else {
  // Proceed with code
}
```


----------



## streetfighter 2 (Oct 28, 2010)

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:

```
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");
    }
  }
```


----------



## stinger608 (Oct 28, 2010)

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 


```
<!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>
```

This specific section of the text book is _*"Writing Custom Error Handling Functions"*_

I guess I am just confused on how this would be re-wrote to my class question LOL.


----------



## stinger608 (Oct 28, 2010)

streetfighter 2 said:


> 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.
> 
> ...



Ah, your first one was Java 
This code makes much more sense LOL. 

Wow guys, thanks for the ultra quick responses!!!


----------



## streetfighter 2 (Oct 28, 2010)

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.


----------



## stinger608 (Oct 28, 2010)

streetfighter 2 said:


> 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.



Holy crap, that is a lot of work to say "Hello World."


----------



## FordGT90Concept (Oct 28, 2010)

stinger608 said:


> ```
> <!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">
> ...


This code is just for formatting/displaying error messages in a popup window; not actually finding/catching input errors.  Again, parseInt and parseFloat are the commands you are looking for.


----------



## stinger608 (Oct 29, 2010)

Thanks a ton guys! Of course I am still somewhat confused, but then again, that's not unusual


----------

