Click to See Complete Forum and Search --> : Powers
neil9999
09-30-2003, 02:28 PM
Hi,
Is it possible to calculate powers using javascript?
I'd like it to do it to do var que to the power of 4.
Eg.
var que='5'
var ans=que to power of 4'
Thanks,
Neil
Khalid Ali
09-30-2003, 02:33 PM
I am sure this resource will guide you to a solution..
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj.html#1008620
tinernet
09-30-2003, 02:35 PM
yes it is, ud do the following:
var que = 5;
var ans = Math.pow(que, 4);
<script type="text/javascript"><!--
x = 10; // any number (base)
y = 5; // any number (exponent)
alert(Math.pow(x, y));
//--></script>
[J]ona
neil9999
10-01-2003, 11:08 AM
Thanks everyone. Using
var que = 5;
var ans = Math.pow(que, 4);
would it be possible to automaticly put commas in the answer (var ans)?
eg 1000000 would become 1,000,000
Thanks again,
Neil
sciguyryan
10-01-2003, 01:47 PM
how could you add to this so that the user could specify the power?
Sciguyryan, untested code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head><title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript"><!--
function givePow(base,power){
alert(Math.pow(base,power));
}
//--></script>
</head>
<body>
<form action="" name="formObj" onsubmit="return false;"><div>
<input type="text" name="base" value="Base Number"><br>
<input type="text" name="power" value="Exponent Number"><br>
<input type="button" onclick="givePow(this.form.base.value,this.form.power.value);" value="Calculate Data">
</div></form>
</body></html>
[J]ona
Originally posted by neil9999
Thanks everyone. Using
var que = 5;
var ans = Math.pow(que, 4);
would it be possible to automaticly put commas in the answer (var ans)?
eg 1000000 would become 1,000,000
You will either use an Regular Expression for that, or you will use a lot of string manipulation that I won't really get into unless the need arises. I prefer RegExps (Regular Expressions), but in this case, I'm a bit short on them and do not know exactly how to get them to work the way you're asking... Sorry.
[J]ona
neil9999
10-02-2003, 03:29 PM
Ok, it isn't too much of a problem, thanks anyway.
Neil
sciguyryan
10-03-2003, 01:48 PM
Jona you are a computer mastermind! how many is it i woe you now? LOL!
Originally posted by sciguyryan
Jona you are a computer mastermind! how many is it i woe you now? LOL!
That's not entirely accurate - I've still not conquered Regular Expressions as my last reply to this post has shown...
[J]ona
sciguyryan
10-03-2003, 02:36 PM
ok, then the next best thing to a computer maseermind, my script works like a dream, on the site we will need your help with things like this!
David Harrison
10-03-2003, 02:51 PM
I don't know how you could use RegExp's to put the comma's in, I normally just use them to replace things.
Instead I have used a while loop:
sciguyryan
10-03-2003, 03:07 PM
how does that work then?! :confused:
David Harrison
10-03-2003, 03:19 PM
What, you mean the while loop? I did this:
while(n<first.length){first=first.substring(0,(first.length-n))+","+first.substring((first.length-n),first.length);n+=4;}
where first is the part of the number before a decimal point, (if there is one).
AdamBrill
10-03-2003, 03:49 PM
You could try this(since it is much simpler):function add_commas(num){
finish = num.substring(0, num.length%3);
for(x=num.length%3+3; x<=num.length; x+=3){
finish+=((finish == "")?"":",")+num.substring(x-3, x);
}
return finish;
}
alert(add_commas("100000000000000"));
David Harrison
10-03-2003, 03:57 PM
Would that take account of decimal places? Also what do those % symbols do?
AdamBrill
10-03-2003, 04:09 PM
No, did he say it was supposed to??? Here is a script that does, though, if he wants it:function add_commas(num){
dec = num.split('.')[1];
num = num.split('.')[0];
finish = num.substring(0, num.length%3);
for(x=num.length%3+3; x<=num.length; x+=3){
finish+=((finish == "")?"":",")+num.substring(x-3, x);
}
return finish+((dec)?"."+dec:"");
}
alert(add_commas("1000000000000000.1234124"));Also, % is the modulous character. It returns what the remainder would be if the two numbers were be divided.
Originally posted by lavalamp
I don't know how you could use RegExp's to put the comma's in...Unfortunatly, JavaScript doesn't have an easy way to reverse a string, so most of what I post below is just turning the string into an array, reversing it , and turning it back into a string (twice), so we can work through it backward (as that is how the commas work). Anyway, here's the regexp version... ;)
<script type="text/javascript">
foo = "1234567.89";
String.prototype.rstring = function() {
return this.split("").reverse().join("");
}
foo = foo.rstring().replace(/(\d{3})(?!\d*\.)/g, "$1,").rstring();
alert (foo)
</script>
My hatred towards Math causes a mind-block in this area... :rolleyes:
Nothing works when it's banged into your head for nine years...
[J]ona
David Harrison
10-03-2003, 06:20 PM
Well I'm sort of inclined to agree with you, although I was fine with it up to the end of year 12, but now I'm in year 13 I'm finding it a bit slow going.
It's not hard... Just frustrating. I just hate dealing with Mathematics, even though I know that they are important.
One day... I'll get to work on some Math. I'm going to be using it a great deal soon anyway... But not in JavaScript - in PHP.
[J]ona
David Harrison
10-03-2003, 06:28 PM
It gets harder if you stick with it, there's so mny different formulas for integrating and differentiting and then there's so many one off cases.
Although, now I'm interested in why you will need a lot of maths in PHP.
everyone should just buy a ti92, with one of those babies, you don't even have to think until calc 3 (i know from experience)
David Harrison
10-03-2003, 06:35 PM
Ok, excuse me for being English but I don't know what those things are. I'm going to hazard a guess that they are a calculator and a test but I could be wrong.
Originally posted by lavalamp
Although, now I'm interested in why you will need a lot of maths in PHP.
Remind me in about a year or so...
Originally posted by lavalamp
Ok, excuse me for being English but I don't know what those things are. I'm going to hazard a guess that they are a calculator and a test but I could be wrong.
I'm pretty sure it's a calculator - I use the TI-83+
[J]ona
neil9999
10-04-2003, 02:08 AM
Thanks to you all. All your scripts are fine (although I couldn't get AdamBrill's last to work) but I went for pyro's last as it's shortest. I don't need any textboxes to enter the numbers in, as the numbers I'll be using are specified in another script.
Thanks again,
Neil
Awesome... Glad it works for you... :)