[英]JavaScript : Split string using function and returning it
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) 尝试了几种想法,但该功能无法正常工作/返回(html代码正常)
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";
parseInt('My Name');
returns NaN
. 返回NaN
。
Remove the parseInt()
, and just keep it as: 删除parseInt()
,并将其保留为:
var naam = prompt('Input your name seperated by a space.');
I could spot 2 problems in your code: 我可以在您的代码中发现2个问题:
1- The parameter name to your function is the same name as that of a global variable . 1- 函数的参数名称与全局变量的名称相同 。 It is probable that any references to 'naam' in your function use the global variable instead of what you pass. 函数中对“ naam”的任何引用都可能使用全局变量而不是您传递的变量。 Regardless, don't do that. 无论如何,不要那样做。
2- parseInt
will take a string and extract an integer out of it and returns a number
. 2- parseInt
将获取一个字符串并从中提取一个整数并返回一个number
。 number
types doesn't have the split()
method and you probably wanted a string containing the name. number
类型没有split()
方法,您可能想要一个包含名称的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.