View Full Version : Help creating javascript to show a range of numbers
di-nerf
12-07-2003, 02:18 AM
I am trying to create a web page that pops up a input box asking for a string in the format "abc123" and the web page then displays all the range of the input adding 1 each time up to a max of 32.
ie,
abc123
abc124
abc125
|
V
abc154
any help would be most appreciated as i am just learning javascript.
x_goose_x
12-07-2003, 06:31 AM
This any help?
<script>
function increment ( str )
{
sRet = "";
for ( x=0; x<32; x++ )
{
sOne = str.substring(0,3);
sTwo = parseInt(str.substring(3))+x;
sRet += sOne+sTwo+"<br>";
}
return sRet;
}
tmp = increment ( "abc123" );
document.write(tmp);
</script>
di-nerf
12-08-2003, 10:55 AM
Thanks goose,
Its almost exactly what i want, is there any way i can get it to pop up an input box asking for the initial value when the page loads??
basically what i want is for the page to have a list already on it
ie,
device 1
device 2
device 3
etc
etc
and for the input box to ask for the initial value ie, abc123, and then when the ok button is clicked the web page then displays
ie,
device 1 abc123
device 2 abc124
device 3 abc125
etc
etc
much appreciated for your help
Willy Duitt
12-08-2003, 11:25 AM
Try this:
<script type="text/javascript">
str = prompt('Enter The Product String','')
function increment (str){
sRet = '';
for ( x=0; x<32; x++ )
{
sOne = str.substring(0,3);
sTwo = parseInt(str.substring(3))+x;
sRet += sOne+sTwo+"<br>";
}
return sRet;
}
tmp = increment (str);
document.write(tmp);
</script>
.....Willy
di-nerf
12-09-2003, 11:45 AM
Excellent Willy thank you very much
glenngv
12-09-2003, 12:48 PM
Just a note...
str.substring(0,3) and parseInt(str.substring(3)) should be outside the for-loop since str is fixed
function increment (str){
if (!str || str.length<4) return;
var sRet = '';
var sOne = str.substring(0,3);
var iSuffix = parseInt(str.substring(3), 10);
for (var x=0; x<32; x++ )
{
sTwo = iSuffix+x;
sRet += sOne+sTwo+"<br>";
}
return sRet;
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.