根据条件更改字符串-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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短