拆分和加入(ES6)[关闭]

问题描述 投票:0回答:0

我试图编写代码,使名字和姓氏的首字母大写,其余字母小写,还使用 split 和 join 进行间距。

let [firstName, lastName]=prompt("What is your name?").split(" ");
firstName=firstName.toUpperCase(firstName).slice(0,1);
lastName=lastName.toUpperCase(lastName).slice(0,1);
firstName=firstName.toLowerCase(firstName).slice(1,firstName.length);
lastName=lastName.toLowerCase(lastName).slice(1,lastName.length);
const newName=[firstName,lastName].join(" ");
console.log(newName);

显示的是“amnik idhu”而不是“Ramnik Sidhu”

javascript join split slice
© www.soinside.com 2019 - 2024. All rights reserved.