根据条件更改字符串-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 好听的元旦简短祝福语