Announcement Announcement Module
Collapse
No announcement yet.
basic javascript question Page Title Module
Move Remove Collapse
Search Search Module
Collapse

Advertisement Advertisement Module
Collapse

Latest Topics Latest Topics Module
Collapse

  • alenna
    Reply to Poster and text legibility/placement/design
    alenna
    Thank you again, I did some adjustments and it does finally feel like I am getting some where. I do appreciate the help, I incorporated some more changes based off your advice. Can be seen in attachment....
    Yesterday, 09:56 PM
  • typo
    Reply to Critique : font pairing
    typo
    Thank you for your feed-back. Actually this it's just a part of the context, a work in progress of the below exposed logotext. It was a must to find something pretty similar in the serif/sans-serif relation...
    Yesterday, 09:29 PM
  • bubby17346
    Reply to teaching prepress to student graphic designers
    bubby17346
    In my GD program, we were required to take a class that was all about print file preparation and preflighting. Granted, it was only one class, one semester, but, from what the rest of you are saying,...
    Yesterday, 08:52 PM
  • plopes20587
    Reply to Personal Branding
    plopes20587
    Thanks i'm really learning a lot from this feedback...
    Yesterday, 08:49 PM
  • salsa
    Reply to teaching prepress to student graphic designers
    salsa
    I'd agree with the OP's statement. I learned design at a vocational college and was taught the bare bones of prepress, I consider myself lucky to have been taught bleeds and trapping and overprinting......
    Yesterday, 08:45 PM

X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • basic javascript question

    how do i make this function display properly?
    The alert do not add the numbers, instead they treat it as strings i guess and alerts "The total value is 5711" when i want it to say "The total value is 23"

    <html>
    <body>

    <script type="text/javascript">

    /* Avoiding errors using typeof operator */

    function addThree(arg1, arg2, arg3)
    {

    if ( (typeof arg1 !="number") || (typeof arg2 !="number") || (typeof arg3 !="number") )
    alert("Please input only numbers!");
    else
    alert("The total value is "+arg1+arg2+arg3);
    }

    var x=5, y=7;
    addThree(x, y , 11);


    </script>

    </body>
    </html>

  • #2
    If you use + on a number, it converts it to a string I think. + is used to concatenate strings, variables, etc.

    However, adding up nothing but integers works fine. What you are doing, is adding a string, then 3 numbers, which wont work.

    HTML Code:
    <script type="text/javascript">
    
    function addThree(arg1, arg2, arg3)
    {
    	if ( (typeof arg1 !="number") || (typeof arg2 !="number") || (typeof arg3 !="number") )
    	{
    		alert("Please input only numbers!");
    	}
    	else
    	{
    		total = arg1+arg2+arg3;
    		alert("The total value is " + total);
    	}
    }
    
    addThree(1,2,3);
    
    </script>

    Comment


    • #3
      I see, so you would have to set the total of the arguments in a variable first and then declare that variable with the string.

      thanx

      Comment

      Mediabistro A division of Prometheus Global Media home | site map | advertising/sponsorships | careers | contact us | help courses | browse jobs | freelancers | content | member benefits | reprints & permissions terms of use | privacy policy Copyright © 2015 Mediabistro Inc.
      Working...
      X