Jump to content
Larry Ullman's Book Forums

pakoch

Members
  • Posts

    7
  • Joined

  • Last visited

Posts posted by pakoch

  1. Hi,

     

    Here is an observation and a question.  I noticed that when a mating curly bracket is left out of the code, like in an "if" statement, nothing on the entire .js page is executed.  

     

    if (something !== undefined)  {

      

       do something

     

    }  else {

     

      do something else

    }

     

    Since the curly bracket that I neglected to type was in the middle of the page, I would assume that at least some of the code before that part of the function would get executed, but that does not seem to be the case. 

     

    Is this normal behaviour for JavaScript?  If so, then the entire page must be analyzed before any of it is acted on.

     

    Thanks,

    Peter Koch

  2. Thanks for the smiley face information Larry; I thought it might be something like that causing the problem.

     

    HarlleySan, your suggestion of using .value worked for me.  However, I am not using the parseInt function now.  Do you still see any reason why the parseInt function should be used in this case?   Here is what I wrote to test isNaN.  I put some extra alert boxes in there so I could see what was happening to the variables, but you can see that I am testing each variable with isNaN.

     

    function isNaNtest() {

      'use strict';

      var number_1 = document.getElementById('number_1');
      var number_2 = document.getElementById('number_2');

      alert('The first number is ' + number_1.value);
      alert('The second number is ' + number_2.value);

      if ((isNaN(number_1.value)) && (isNaN(number_2.value))) {
       alert('Both inputs are Not a Number');
      }
      else if ((isNaN(number_1.value)) && (!isNaN(number_2.value))) {
       alert('The 1st input is NaN, the 2nd input is OK');
      }
      else if ((!isNaN(number_1.value)) && (isNaN(number_2.value))) {
       alert('The 1st input is OK, the 2nd input is NaN');
      }
      else if ((!isNaN(number_1.value)) && (!isNaN(number_2.value))) {
       alert('Both inputs are OK');
      }
      else {
       alert('Unknown');
      }
    }
    function init() {
       'use strict'
       document.getElementById('theForm').onsubmit = isNaNtest;
      }
    window.onload = init;

  3. Do you mean like this:

     

    var a = document.getElementById('number');
    var b = parseInt('a',10);

    if (isNaN( b ) {
    alert('Not a number');
    }
    else {
    alert('Is a number');
    }

     

    Because for some reason this also does not work like it should.  I also did a test where I tried to display the length of variable b and it comes back undefined. 

    Thanks,

    Peter

     

    Also, I don't understand why I can't get rid of the smily face; it just keeps popping up?  There should be a "b" in place of it.

  4. Thanks, I read through the material and it helps.  However, I am still very new to this and I must be missing something.  I am trying to use parseInt to convert the user entered number, which is actually a string, into an integer.  Then I want to check it using the isNaN function to be sure the user actually entered a number.   Regardless of whether I enter a number or letters, it is evaluated as NaN.  Here is what I am working with:

     

    var a = document.getElementById('number');
      parseInt('a',10);

      if (isNaN(a)) {
       alert('Not a number');
      }
      else {
       alert('Is a number');
      }

     

    Am I using the parseInt funcation incorrectly?  Thanks for your help.

     

    Peter

  5. Hi,

     

    When testing a user input to see if it is a number, just using isNaN() function does not appear to work without doing some kind of conversion to the input.  Here is a snippet of what I am working on:

     

    var number = document.getElementById('number');

      if (isNaN(number)) {
       alert('Not a number');
      }
      else {
       alert('Is a number');
      }

     

    Regardless of whether a letter or number is entered by the user, the isNaN function is true and the first alert is posted.

     

    Can someone tell me if I am correct?  and if so, what conversion needs to be done on the 'number' input to get it to be properly evaluated by the isNaN function? 

     

    Thank You,

    Peter Koch

  6. Hi, I am just starting to learn Javascript and I have a quick question about these statements.

     

    total -= discount;

     

    total *= tax;

     

    Translated to English, does the first one mean: The total minus the discount equals some value?

    The second one: The total multiplied by the tax equals some value?

     

    Thanks,

     

    Peter Koch

×
×
  • Create New...