根据条件更改字符串-JavaScript
我们需要编写一个包含字符串的JavaScript函数。我们函数的任务是根据以下条件更改字符串-
如果字符串中的第一个字母为大写字母,则应将完整的字符串更改为大写字母。
否则,我们应将完整字符串更改为小写字母。
示例
以下是代码-
const str1 = "This is a normal string";
const str2 = "thisIsACamelCasedString";
const changeStringCase = str => {
let newStr = '';
const isUpperCase = str[0].charCodeAt(0) >= 65 && str[0].charCodeAt(0) <= 90;
if(isUpperCase){
newStr = str.toUpperCase();
}else{
newStr = str.toLowerCase();
};
return newStr;
};
console.log(changeStringCase(str1));
console.log(changeStringCase(str2));输出结果
以下是控制台中的输出-
THIS IS A NORMAL STRING thisisacamelcasedstring
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短