根据条件更改字符串-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
热门推荐
2 天仙配简短祝福语
10 自由 生日祝福语简短独特
11 兔年简短祝福语8字
12 新年祝福语大全简短成语
13 高考成绩出祝福语 简短
14 集体生日祝福语简短的
15 周一祝福语简短精辟
16 官宣祝福语简短霸气
17 对新人祝福语大全简短
18 婚礼伴娘祝福语大全简短