![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #2 |
|
Master Coder ![]() Join Date: Jun 2002
Location: London, England
Posts: 7,092
Thanks: 85
Thanked 836 Times in 817 Posts
![]() ![]() |
Google is your friend!
http://www.javascriptkit.com/script/...nconvert.shtml http://javascript.internet.com/misce...o-decimal.html It is your responsibility to die() if necessary….. - PHP Manual Last edited by Philip M; 11-04-2009 at 06:27 PM.. |
|
|
|
|
|
PM User | #3 |
|
Regular Coder ![]() Join Date: Aug 2006
Location: FL
Posts: 655
Thanks: 1
Thanked 121 Times in 118 Posts
![]() |
Alternative code, just because I working and am a slow typist.
Code:
<html>
<head>
<title>Binary Conversion</title>
<script type="text/javascript">
// For: http://codingforums.com/showthread.php?t=181347
function Dec_Bin(val) { return parseInt(val,10).toString(2); }
function Bin_Dec(val) { return parseInt(val,2).toString(10); }
</script>
</head>
<body>
Binary: <input type="text" id="bin" value='1010'>
<button
onclick="document.getElementById('dec').value=Bin_Dec(document.getElementById('bin').value)"> Bin to Dec</button>
<button onclick="document.getElementById('bin').value = ''">Clear</button>
<br>
Decimal: <input type="text" id="dec" value=''>
<button
onclick="document.getElementById('bin').value=Dec_Bin(document.getElementById('dec').value)"> Dec to Bin</button>
<button onclick="document.getElementById('dec').value = ''">Clear</button>
</body>
</html>
|
|
|
|
| Users who have thanked jmrker for this post: | kasu (11-05-2009) |
|
|
PM User | #4 |
|
New to the CF scene Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
![]() |
Thanks a lot for the reply..but the problem I have is how do you do it using "onchange" attribute rather than using buttons..and converting either decimal or binary to a hexadecimal...working all this time but did not get it ryt!!
Last edited by kasu; 11-04-2009 at 11:19 PM.. |
|
|
|
|
|
PM User | #5 |
|
Senior Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
![]() ![]() |
The only things important to know are that you can convert text to number using any radix (base) using:
Code:
var num = parseInt( text, radix ); To convert *from* a number to text in the proper base/radix, you just put the radix in the toString(): Code:
var text = num.toString( radix );
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
| Users who have thanked Old Pedant for this post: | kasu (11-05-2009) |
|
|
PM User | #8 |
|
Senior Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
![]() ![]() |
Code:
<form>
Enter decimal:
<input name="decimal" onchange="this.form.binary.value=(parseInt(this.value,10)).toString(2);">
<hr>
Enter binary:
<input name="binary" onchange="this.form.decimal.value=(parseInt(this.value,2)).toString(10);">
</form>
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
| Users who have thanked Old Pedant for this post: | kasu (11-05-2009) |
|
|
PM User | #9 |
|
New to the CF scene Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
![]() |
how do you perform this coding with a for loop..
<html> <head> <title> Dice Stats </title> <script type="text/javascript" src="http://random.js"> </script> <script type="text/javascript"> function RollRepeatedly() // Assumes: repsBox contains a number // Results: simulates that many dice rolls, displays # of doubles { var doubleCount, totalRolls, repCount, roll1, roll2; doubleCount = 0; // INITIALIZE THE COUNTER totalRolls = document.getElementById("repsBox").value; totalRolls = parseFloat(totalRolls); repCount = 0; while (repCount < totalRolls) { // REPEATEDLY: roll1 = RandomInt(1, 6); // SIMULATE THE DICE ROLLS roll2 = RandomInt(1, 6); if (roll1 == roll2) { // IF DOUBLES, doubleCount = doubleCount + 1; // INCREMENT THE COUNTER } repCount = repCount + 1; // INCR. REPETITION COUNTER } document.getElementById("countBox").value = doubleCount; // display # } </script> </head> <body> <div style="text-align:center"> <h2>Dice Stats</h2> <p> Desired number of rolls = <input type="text" id="repsBox" size="6" value="1000" /> </p> <p> <input type="button" value="Click to Roll" onclick="RollRepeatedly();" /> </p> <p> Number of doubles obtained = <input type="text" id="countBox" size="6" value="0" /> </p> </div> </body> </html> Last edited by kasu; 11-09-2009 at 10:40 PM.. |
|
|
|
|
|
PM User | #10 |
|
Regular Coder ![]() Join Date: Aug 2006
Location: FL
Posts: 655
Thanks: 1
Thanked 121 Times in 118 Posts
![]() |
Beginning to look like a homework assignment ...
See: http://www.webdeveloper.com/forum/sh...d.php?t=219590 |
|
|
|
|
|
PM User | #11 |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,066
Thanks: 30
Thanked 143 Times in 139 Posts
![]() |
I guess Old Pedant already gave you all the information you need.
To convert a value from a base to decimal: Code:
parseInt(value,base) Code:
value.toString(base)
__________________
KOR -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Last edited by Kor; 11-09-2009 at 04:18 PM.. |
|
|
|
|
|
PM User | #12 |
|
New to the CF scene Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
![]() |
hi wondering a start to this ...just give me a idea plzz to create the following
* *** ***** ******* ***** *** * * * * * * * * * * * * * the above diagram is displayed incorrectly on the thread..plzz refer the page source of this page LINE 3105 COlUMN 3 to get the correct picture Last edited by kasu; 11-19-2009 at 04:23 AM.. |
|
|
|
|
|
PM User | #13 |
|
Senior Coder ![]() Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
![]() ![]() |
LOL! So you and Dartz101 are in the same class:
http://www.codingforums.com/showthread.php?t=182173 Tell me, which of you is getting the better grade? And, I also answered this last question from yet another student in your same class: http://www.codingforums.com/showthread.php?t=182091 He seems to be doing better than either you or Dartz101, no? Where the heck is this class, anyway??
__________________
"Old age and cunning win out over youth and enthusiasm every time." |
|
|
|
|
|
PM User | #14 |
|
New to the CF scene Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
![]() |
i have no idea as to who is dartz101...and i checked the link uve posted but it does not have any relation to what my question is..and the second link aint workin...
.. I just wanted to create a diamond using asterix ....and i hope some one give me an idea to start off..i dont want any of u 2 do my homework..but hope a help from some one ..that will be great coz am a beginner..
|
|
|
|
|
|
PM User | #15 | |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,066
Thanks: 30
Thanked 143 Times in 139 Posts
![]() |
Quote:
2. We hardly wait to see what you have done so far - your code I mean - in order to help you.
__________________
KOR -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|