根据条件更改字符串-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 母亲说生日祝福语 简短