Javascript 连接 FirstName、LastName 和代码
Javascript Concatenate FirstName, LastName and Code
我正在尝试将两个名称的第一个字母连接到随机生成的代码上。
var firstname = prompt("Please enter your first name.");
var lastname = prompt ("Please enter your last name.");
if (amountCorrect >= 4){
alert("Your login Code for the store is: " + str(firstname,1,1) + str(lastname,1,1) + (generateCode())); // Do the generateCode function
}
else{
alert("You have not passed on this occasion. You will now be taken back to the homepage.");
window.history.go(-1); // Go back a step
}
}
function generateCode(){
var text = "";
var possible = "0123456789";
for( var i=0; i < 4; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
警报行中没有使用"str"的定义。
alert("Your login Code for the store is: " + firstname[0] + lastname[0] + (generateCode()));
以上应该有效。看起来你在 if 块之后也有一个杂散的 }。您可能还应该对用户输入进行更多的错误检查,例如,如果用户在提示符下没有输入值,您将获得未定义未定义####,其中####是生成的代码。
您可以使用
substring
提取第一个字符:
alert("Your login Code for the store is: " + firstname.substring(0,1) + lastname.substring(0,1) + (generateCode()));
这是String.prototype.substring
的文档。
替换该行
alert("Your login Code for the store is: " + str(firstname,1,1) + str(lastname,1,1) + (generateCode())); // Do the generateCode function
跟
alert("Your login Code for the store is: " + firstname.substring(1, 0) + lastname.substring(1, 0) + (generateCode())); // Do the generateCode function
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- 代码不会验证
- 如何从rails中的代码中删除新行( )
- Angularjs代码未在匿名函数中运行
- 如何做到这一点,使代码在不传递条件后执行函数
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 面向对象的Javascript代码在IE7中不起作用
- 将 jQuery 代码添加到 Index.aspx 页面
- 为什么忽略了eval()代码中的语法错误
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- Javascript 连接 FirstName、LastName 和代码