我试图编写一个代码,将句子的一半变成大写,另一半变成小写,但是没有用。需要帮助,这是我的代码。
const fullClintStr = prompt("please enter text: ");
const strlen = fullClintStr.length;
const lowerCaseStr = fullClintStr.toLowerCase();
const upperCaseStr = fullClintStr.toUpperCase();
const MiddleSentence = (strlen/2).toFixed(0);
for (let i = 0; i < MiddleSentence; i++) {
var finalSTR = upperCaseStr[i].toUpperCase(upperCaseStr[i]);
}
window.alert(finalSTR);
您可以使用slice
method来获取所需的内容,而不必进行循环。另外,您的循环无法正常工作,因为您已finalStr
重新分配了最新角色。
同样,绝对有一个范围可以防止对整个字符串进行小写和大写,而只需对left and right substrings
和进行添加,然后将它们都添加在一起即可finalStr
像@Tim一样进行操作。
const fullClintStr = prompt("please enter text: ");
const strlen = fullClintStr.length;
const lowerCaseStr = fullClintStr.toLowerCase();
const upperCaseStr = fullClintStr.toUpperCase();
const MiddleSentence = (strlen/2).toFixed(0);
let finalSTR = lowerCaseStr.slice(0,MiddleSentence) + upperCaseStr.slice(MiddleSentence, strlen);
window.alert(finalSTR);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句