0

I don't know what I'm doing wrong here. Tried several thinks but the function doesn't work/return properly (The html code is okay)

var divResult = document.getElementById("divResult");
var naam;

function splitsen(naam){
    var res = naam.split(" ");
    document.write(res[0]);
    var voornaam = res[0];
    var achternaam = res[1];
    var tnaam = [voornaam, achternaam];
return tnaam;
}

naam = parseInt(prompt("Geef je voornaam en achternaam in gescheiden met een spatie"));

var voornaam = splitsen(naam)[0];
var achternaam = splitsen(naam)[1];
divResult.innerHTML = "oefening 8";
divResult.innerHTML += "Voornaam: " + voornaam;
divResult.innerHTML += "Achternaam" + achternaam;
divResult.innerHTML += "Email: " + voornaam + "." + achternaam + "@student.arteveldehs.be";
1
  • well your function work it return 2 words in an array Commented Mar 2, 2016 at 20:00

2 Answers 2

1

parseInt('My Name'); returns NaN.

Remove the parseInt(), and just keep it as:

var naam = prompt('Input your name seperated by a space.');
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. If you would accept my answer, I'd be grateful.
0

I could spot 2 problems in your code:

1-The parameter name to your function is the same name as that of a global variable. It is probable that any references to 'naam' in your function use the global variable instead of what you pass. Regardless, don't do that.

2-parseInt will take a string and extract an integer out of it and returns a number. number types doesn't have the split() method and you probably wanted a string containing the name.

7 Comments

@MaxMastalerz What are you talking about?
Nevermind my first comment. What I meant to say is that the parameter name can be the same as the global variable. It won't matter.
Yes, but still. That can cause ambiguity from a developer's point of view.
He doesn't even need a parameter. naam is a global, and therefore accessible in the function without it being passed in.
So, I see you agree using a global variable's name for a function parameter is bad.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.