...

View Full Version : Array undefined problem



jdruwe
01-05-2012, 12:09 PM
Hey guys

I got a problem with my program, it converts a name to initial

example: Jordy Derp ==> J.D. but i always get an undefined in front of it

http://i43.tinypic.com/2qu12jn.png


addEventListener("load", init, false);

function init() {


var naam = new initialen(prompt("geef een naam in"));
var tijdelijk = ""
var naamArray = new Array();
var perm = ""

function initialen(naam) {

if (naam.indexOf(' ') >= 0) {
naamArray = naam.split(" ")


for (var i = 0; i < naamArray.length; i++) {
perm += naamArray[i].charAt(0) + "."
}
alert(perm)

}
else {
alert("False")
}
}
}

btw it also checks for spaces so u need to type 2 names


Thanks in advance

Amphiluke
01-05-2012, 12:22 PM
Declare the perm variable before you invoke the initialen() function.

P.S. To be exact, the variable must be initialized before the function will be invoked.

jdruwe
01-05-2012, 12:44 PM
Declare the perm variable before you invoke the initialen() function.

P.S. To be exact, the variable must be initialized before the function will be invoked.


var perm = "" is before the function?

Amphiluke
01-05-2012, 12:47 PM
Before the function invoking.


var perm = ""
var naam = new initialen(prompt("geef een naam in"));
var tijdelijk = ""
var naamArray = new Array();

jdruwe
01-05-2012, 12:50 PM
Before the function invoking.


var perm = ""
var naam = new initialen(prompt("geef een naam in"));
var tijdelijk = ""
var naamArray = new Array();


Thanks it works now , stupid mistake ^^

Philip M
01-05-2012, 01:07 PM
<script type = "text/javascript">

var naam = prompt("Geef een naam in","");
naam = naam.replace(/[^a-z\s]/gi,""); // strip all but letters and spaces
naam = naam.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
naam = naam.replace(/\s{2,}/g," "); // replace multiple spaces with one space

var perm = "";

if (naam.indexOf(' ') >= 0) {
var naamArray = naam.split(" ");

for (var i = 0; i < naamArray.length; i++) {
perm += naamArray[i].charAt(0) + "."
}
perm = perm.toUpperCase();
alert (perm);
}
else {alert("False")}

</script>


Customer in UK travel agent shop: - "What month is it now in Australia?"

Amphiluke
01-05-2012, 02:36 PM
It seems to me that there is no need in loops at all… =)

var re = /\s*(\w)\S*[\s$]*/g;
var str = " Irvim John Kenneth Loyd Martin Nero Oliver Paul Quincy ";
alert(str.replace(re, "$1.").toUpperCase());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum