1

am passing variable like fun1(this, 1/0) but am getting values like fun1(this, infinity) in jquery.please advise me how to resove this.

the code as follows:

1.passing parameter

<input type="button" id="cutdel" onclick="javascript:cutdelete(this,2/0)"/>

2.function in use

 function cutdelete(button, locGauge) {
     $.blockUI({
         message: $('#deletewire'),
         css: {
             width: '360px',
             top: ($(window).height() - 1000) / 2 + 'px',
             left: ($(window).width() - 360) / 2 + 'px',
             backgroundColor: 'none',
             border: '3px solid #aaa',
             padding: '10px'
         }
     });
     $('#delete').one('click', function() {
         loadCart = true;
         for (var i = 0; i < selectedgaugeArray.length; i++) {
             if (selectedgaugeArray[i] == locGauge) {
                 selectedgaugeArray.splice(i, 1);
                 $(button).parent().parent().remove();
                 break;
             }
         }
         alert(selectedgaugeArray);
         $.unblockUI();
         return false;
     });
     $('#save').click(function() {
         $.unblockUI();
         return false;
     });
 }

here am passing locGuage as 1/0 , 2/0 ,1:3 but am getting values as infinity. but it working for integer values.

hi all... i am rendaring html content in script itself like

content = content + "<tr class=\"selitems\"><td><input  type=\"button\" id=\"cutdel\" style=\"background: #FFFFFF url(images/cutclose.png); width: 14px;height: 14px;border: none;text-align: center;padding: 5px;font-size: 12px;\" onclick=\"javascript:cutdelete(this,"+WireGuage+")\">"+"</input></td>"

here am appending locGuage as onclick=\"javascript:cutdelete(this,"+WireGuage+")\"> in content. so tel me how to convert that as string

Answer:

i passes variable as string

that is: i rendered locguage variable like

     var WireGuage ='\''+ $("#WireGuage_"+p).val()+'\'';    

the problem solved.

thank u all... thanks for your time.

Abhi Urs
  • 95
  • 3
  • 8
  • 1
    Use `onclick="javascript:cutdelete(this,'2/0')"`, pass argument as string. – Satpal Dec 24 '14 at 11:07
  • 1
    `2/0` is a division by zero operation... pass it as a string like `'2/0'` – Arun P Johny Dec 24 '14 at 11:08
  • why do you want to divide an integer with zero ? (as you know in math class, divide by zero will result infinity) – whale_steward Dec 24 '14 at 11:12
  • @Satpal—there is no need for `javascript:` in an intrinsic event attribute or DOM property, it's interpreted as a [*label*](http://ecma-international.org/ecma-262/5.1/#sec-12.12). – RobG Dec 24 '14 at 11:41
  • @RobG, Totally agreed with you. I just used OPs code. – Satpal Dec 24 '14 at 11:44
  • This has nothing to do with jQuery. Use something like: `content + "... onclick=\"cutdelete(this,'"+WireGuage+"' ...";`, note the additional single quotes and removal of `javascript:`. – RobG Dec 24 '14 at 11:46

2 Answers2

4

Division by zero is resulting in infinity (as you might remember from your math class).

See:

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • you are correct Chankey Pathak but how to pass variable as string javascript. we all take as var right. – Abhi Urs Dec 24 '14 at 11:22
  • You are passing it like `fun1(this, 1/0)`, change it to ` fun1(this, '1/0')` – Chankey Pathak Dec 24 '14 at 11:26
  • Actually, division by zero is undefined, but in some floating point arithemetics implementations it has a well-defined result. See https://en.wikipedia.org/wiki/Division_by_zero – Lars Nyström Nov 03 '16 at 14:25
3

1/0 is an arithmetical operation that doesn't have a solution.

In most programming languages, it would be a DivisionByZeroException.

In JavaScript, though, you get Infinity. Which is not correct in Maths, as I understand, because if you would multiply Infinity * 0 you won't get your 1 back (think about 6/2 = 3, if you multiply 2 * 3 you DO get 6). But it makes a little sense since, the most the denominator is closer to zero, the results tends to Infinity.

Maybe you can pass your value as a String, as in '1/0'.

Rafael Eyng
  • 4,720
  • 2
  • 33
  • 34
  • You get infinity because **it is** the correct math for it. OP, as the answerer says, just pass it as a string so javascript doesn't try to "solve" it. – tfrascaroli Dec 24 '14 at 11:15
  • 1
    @TIMINeutron If you assume that, in Math, 1/0 = Infinity, and also 2/0 = Infinity, and also 3/0 = Infinity, then you are assuming that Infinity * 0 = 1, but also Infinity * 0 = 2 and Infinity * 0 = 3. I don't think that Math supports that. – Rafael Eyng Dec 24 '14 at 11:17
  • @TIMINeutron—in mathematics, the result of division by zero is undefined since there is no number that can be multipled by zero to get the original numerator. – RobG Dec 24 '14 at 11:39
  • The indetermination is not when you divide by 0 (since its limit _will_ yield infinity), it's when you do infinity * 0, because (as you both have said) you don't know what number it will yield (if not infinity). Then you have to do limits to recover the value. Think about asymptotes and you will get it. It is true that it's not _equal_ to infinity. It's also true it's **not** undefined, since it's _tending_ to infinity at that point. – tfrascaroli Dec 25 '14 at 11:00
  • As I understand it, it is not indetermination, it has no meaning. Indetermination is 0/0, where any number multiplied by 0 (the denominator) will result in 0 (the numerator). So, 0/0 is indeterminate because it can be any value. 1/0 is impossible because there is no value that satisfies. – Rafael Eyng Dec 26 '14 at 17:58