我试图创建一个程序来接受你的生日(yymmdd),用字符串替换数字并输出你的真实birthname"。
问题是循环只输出键入提示的最后一个数字的字符串,+ =输出自身。我有点希望它会遍历循环并在birthname变量中添加一个接一个的字符串。
(这是我的第一个自己的代码,所以要温柔):)
<object>
答案 0 :(得分:0)
您可以在循环中使用它 birthName + =“你想要的”
答案 1 :(得分:0)
var birthDate = prompt("write your birthdate!");
var birthDateSplitter = birthDate.split("");
var len = birthDateSplitter.length;
var birthName = '';
var i = 0;
for (i; i < len; i++) {
if (birthDateSplitter[i] == 9) {
birthName += "do";
} else if (birthDateSplitter[i] == 8) {
birthName += "ka";
} else if (birthDateSplitter[i] == 7) {
birthName += "mu";
} else if (birthDateSplitter[i] == 6) {
birthName += "ssa";
} else if (birthDateSplitter[i] == 5) {
birthName += "tto";
} else if (birthDateSplitter[i] == 4) {
birthName += "ti";
} else if (birthDateSplitter[i] == 3) {
birthName += "le";
} else if (birthDateSplitter[i] == 2) {
birthName += "pa";
} else if (birthDateSplitter[i] == 1) {
birthName += "bu";
} else {
birthName += "vi";
}
}
alert("Your real birthname is " + birthName);
也许&#34; switch-case&#34;这将是一个更好的解决方案
答案 2 :(得分:0)
if (birthDateSplitter[i] == 9) {
birthName += "do";
} else if (birthDateSplitter[i] == 8) {
birthName += "ka";
//...
但更好的是,你可以摆脱if
s:
var birthDate = prompt("write your birthdate!");
var birthDateSplitter = birthDate.split("");
var len = birthDateSplitter.length;
var birthName = '';
var i = 0;
var birthNameParts = ['vi', 'bu', 'pa', 'le', 'ti', 'tto', 'ssa', 'mu', 'ka', 'do'];
for (i; i < len; i++) {
birthName += birthNameParts[birthDateSplitter[i]];
}
答案 3 :(得分:0)
以下是我将如何处理:
var birthDateSplitted = prompt('write your birthdate!').split(''),
nameParts = ['vi', 'bu', 'pa', 'le', 'ti', 'tto', 'ssa', 'mu', 'ka', 'do'],
birthName = birthDateSplitted.map(function(n) { return nameParts[n]; }).join('');
alert('Your real birthname is ' + birthName);