473,699 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why math wont work?

sorry for the simple question, haven't done this in a while. when I use the
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt, it displays the result as 721
instead of the answer I want which is 28. what am I doing wrong.
hanks -Allen Thompson

<html>
<body>
<script language="javas cript">
var y=window.prompt ();
var x=y+3*7;
document.write( x);
</script>
</body>
</html>
Jul 20 '05
19 2065
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Douglas Crockford wrote:
prompt() returns a string.


It returns a string if the user confirms. Otherwise
it returns `null', no matter what has been typed.


That is browser dependent. Opera returns "undefined" , not "null".


*Are* *you* *really* *really* *sure*? [psf 1.1]

My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.

Done the following tests in the Location Bar:

javascript:aler t(prompt("bla") ) // `null' if canceled
javascript:aler t(typeof prompt("bla")) // `object' if canceled
PointedEars
Jul 20 '05 #11
Dauber! wrote:
Waaaaay back on 13-Dec-03 11:26:28, Douglas Crockford said this about Re: why math wont work?:
The recommended, if not even standardized, maximum is 78 characters
per line. Besides, about 74.7% of your attribution is composed of
superfluous information.
prompt() returns a string. Before using the '+' operator, you need to convert
the string to a number.

var x = (+y) + 3 * 7;


That'll still give the same result, unfortunately.


Not in my UAs. But I think you do not
want to be taken seriously, so go away.
Whatcha need to do is this;

var x = parseInt(y)+3*7 ;

That should work.
See the FAQ about parseInt(...).

And of course it will work, but the above
is about 6 times faster and more reliable.
--


The trailing space is missing.
PointedEars
Jul 20 '05 #12
In article <80************ **************@ verizon.SPAMISF OREATING.net.in valid>,
"Dauber!" <da*****@verizo n.SPAMISFOREATI NG.net.invalid> writes:
var x = (+y) + 3 * 7;
That'll still give the same result, unfortunately. Whatcha need to do is
this;


Did you bother testing that before spouting off that it would give the same
results?
It is *well* known in this group that +y where y is a number in a string format
is quicker at converting it to a number than parseInt is, which you use
incorrectly.

var x = parseInt(y)+3*7 ;

That should work.


As long as y doesn't start with a 0 or 0x format.

http://www.jibbering.com/faq/#FAQ4_12

--
Randy
Jul 20 '05 #13
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
*Are* *you* *really* *really* *sure*? [psf 1.1]
*Yes* *I* *am*!
My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.
My Opera/7.23 (Windows NT 5.1; U) [en] returns 'undefined'
javascript:aler t(typeof prompt("bla")) // `object' if canceled


on this exact test.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #14
Waaaaay back on 14-Dec-03 19:16:53, HikksNotAtHome said this about Re: why math wont work?:
var x = (+y) + 3 * 7;
That'll still give the same result, unfortunately. Whatcha need to do is
this;

Did you bother testing that before spouting off that it would give the same
results?
Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.
It is *well* known in this group that +y where y is a number in a string
format is quicker at converting it to a number than parseInt is, which you
use incorrectly.


Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification, indicates that it's perfectly valid; in fact, I'm given
several overloaded versions tht account for "0x" format as well as binary
and octal...

--
da****@banana-and-louie.org
* ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

Jul 20 '05 #15
"Dauber!" <da*****@verizo n.SPAMISFOREATI NG.net.invalid> writes:
Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.
Could you be persuaded to tell us what browsers those are?

If prefix plus isn't working consistently accross browsers, we should
ofcourse make a note of it.
The expected behavior (from the ECMAScript standard) is to be equivalent
to calling the Number function, but without the overhead of a function call.
Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification, indicates that it's perfectly valid;
It is valid.
in fact, I'm given several overloaded versions tht account for "0x"
format as well as binary and octal...


Overloaded?

Anyway, it is the collective experience in this group, that people
using parseInt most likely expect it to convert from base 10. That is
why we recommend also passing the radix to the function, to avoid
surprises when a user entered number starts with zero.

If you know what you are doing, using parseInt without the radix is
correct. It's just not good advice to a beginner without telling why
"089" converts to 0 in some browsers and 89 in others.

Not all browsers accept octal numbers as arguments to parseInt (as,
indeed, they shouldn't according to ECMA 262). But IE does. So any way
you put it, omitting the radix will make your page behave inconsistently
across browsers.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #16
In article <99************ **************@ verizon.SPAMISF OREATING.net.in valid>,
"Dauber!" <da*****@verizo n.SPAMISFOREATI NG.net.invalid> writes:
var x = (+y) + 3 * 7;

That'll still give the same result, unfortunately. Whatcha need to do is
this;
Did you bother testing that before spouting off that it would give the same
results?


Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.


So you have a browser that doesn't convert a string to a number using + ?
Ex:

x = "3";
y = +x;
alert(typeof(y) ); //browser that alerts string instead of number??

I am not in a position to doubt you about it, would be curious to know of it
though. I know of at least 130 browsers so one might not convert it to number.
If it doesn't, then it would definitely be worth knowing about.
It is *well* known in this group that +y where y is a number in a string
format is quicker at converting it to a number than parseInt is, which you
use incorrectly.


Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certificatio n, indicates that it's perfectly valid; in fact, I'm given
several overloaded versions tht account for "0x" format as well as binary
and octal...


valid in the sense of a limited subset of numbers, it works, then yes its
valid.

alert(parseInt( '08'))
And you get 0, not 8.
The incorrectness of your use was the lack of a radix (base 10).

<URL:
http://msdn.microsoft.com/library/de...n-us/jscript7/
html/jsmthparseint.a sp />
--
Randy
Jul 20 '05 #17
Waaaaay back on 15-Dec-03 07:31:06, HikksNotAtHome said this about Re: why math wont work?:
Uhhh....yes . Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.
So you have a browser that doesn't convert a string to a number using + ?
Ex: x = "3";
y = +x;
alert(typeof(y )); //browser that alerts string instead of number??
The latest Voyager for Amiga. Yeah, I know, the vast majority of users don't
use Amiga, but I'm a very stubborn advocate for universality. :) Not the
greatest browser in the world, either...fast, but not great.
Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification , indicates that it's perfectly valid; in fact, I'm given
several overloaded versions tht account for "0x" format as well as binary
and octal...

valid in the sense of a limited subset of numbers, it works, then yes its
valid.


DUH!....forgot the "10" parameter. <blush>

--
da****@banana-and-louie.oops.org
* ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

Jul 20 '05 #18
"Dauber!" <da*****@verizo n.SPAMISFOREATI NG.net.invalid> wrote in message
news:1147.479T1 499T6255001da** ***@verizon.SPA MISFOREATING.ne t.invalid...
<snip>
So you have a browser that doesn't convert a string to
a number using + ?
Ex:
x = "3";
y = +x;
alert(typeof( y)); //browser that alerts string instead of number??


The latest Voyager for Amiga.


Does it claim its script language implementation is ECMA 262 compliant?
If not why not (the current version has been around since 1999) and if
they do, have they been told they have a bug to fix?

How do the other type-converting math operators perform? Does y = -x;
also return a string?
Yeah, I know, the vast majority of users don't
use Amiga, but I'm a very stubborn advocate for
universality . :) ...

<snip>

You will find that the majority here are also advocates of universality,
but if a browser manufactures decides to implement there own scripting
language without regard for either the published standard or precedence
and then tries to execute it when provided with JavaScript/ECMA Script
source code there is only so much that can be done.

Richard.
Jul 20 '05 #19
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.


My Opera/7.23 (Windows NT 5.1; U) [en] returns 'undefined'
javascript:aler t(typeof prompt("bla")) // `object' if canceled


on this exact test.


Straynge. [psf 4.15]
PointedEars :)
Jul 20 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
19064
by: David Eppstein | last post by:
Why doesn't this work? >>> import math >>> math.exp(1j*math.pi) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: can't convert complex to float; use e.g. abs(z) The expected answer, of course, is -1.
8
2488
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a - b. But for float the results would have to be normalize some how to a 32bit range. I understand there would be percision errors. Thanks Tom
17
3621
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels deep. In that case you need much more ram than a PC has to store functions calculated in loops so that you do not have to recalculate every time you cycle through the nest of loops. Using a HD for storage to extend ram is much too slow for many...
6
8770
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between Math.ceil/floor and parseInt +/- 1 to ensure the amount to move was at least 1 and in the right direction. I made a small test to see which one is faster and also included simply adding/subtracting 1. parseInt generally took 50% to 100% longer than...
5
2252
by: Ark | last post by:
Hi everyone, Does anyone know if Direct3D overloads System.Math functions? Also is it possible to access the base functions of the overloaded function (in other words restore original of the overlaoded function)? Thank you
110
8561
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm almost halfway through writing the over 200 functions needed to implement C99's version of math.h, and I would like to have some feedback and/or expert advice on my implementations. Sincerely, Gregory Pietsch
4
22049
by: pdlemper | last post by:
Have carefully installed Python 2.5.1 under XP in dir E:\python25 . ran set path = %path% ; E:\python25 Python interactive mode works fine for simple arithmetic . Then tried >> import math Get error Name error : name 'sqrt' is not defined Same thing with sin(x) . I'm unable to find "math" , "sqrt" , or "sin" anywhere in lib , Libs or include directories . The Tutorial does not clear this up . Please help. Thanks Dave ...
2
2654
by: Helmer | last post by:
i am in programming class trying to make an applet involving random placement of colored blocks, but an error is created when i try to run it saying "no method named "random" was found in type "Math". " i have imported java.awt like so: import java.awt.*; and this is the random number statement: g.fillRect((int)(Math.random()*getSize().width),(int)(Math.random()*getSize().height),20,20); any help you can provide will be greatly...
5
13535
by: aguirre.adolfo | last post by:
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle "An Introduction to Computer Science" where he uses a "import math" statement to calculate a square...
0
8617
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8884
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7751
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.