Javascript code required

uplink6001

Beta member
Messages
2
Good Afternoon.

I need help with some code please. I need to create a string of underscore characters and the string has to be the same length as another existing string or an element in an array.

So for example if I have the following......

A string 'hello world' or wordArray[0] = 'hello world'

....then I need to create a new string consisting of 11 underscore characters or however many characters are in the initial string or array element.

Hope you can help as I can't find any easy way to do this.

Thanks

BG
 
you are going to need it to print an underscore for each character in the word array.
and welcome to CF
 
In the new string, you are going to have to add an underscore for each character in the string value
 
In the new string, you are going to have to add an underscore for each character in the string value

:rolleyes:


Here you go dude;
Code:
<script type="text/javascript">
function replace() {
	var string = document.getElementById('string').value;
	var len = string.length;
	var underscores = new Array();
	for (var i = 0; i < len; i++) {
		underscores[i]="_";
	}
	document.getElementById('spacers').innerHTML=underscores.join("");
}
</script>
<button onclick="replace()">Replace</button>
<input type="text" id="string" value="one">
<span id="spacers"></span>
 
Back
Top Bottom