将字符串转换为二进制字符串-JavaScript
我们需要编写一个JavaScript函数,该函数接受小写字符串并返回一个新字符串,其中[a,m]之间的所有元素都用0表示,[n,z]之间的所有元素都用1表示。
示例
以下是代码-
const str = 'Hello worlld how are you';
const stringToBinary = (str = '') => {
const s = str.toLowerCase();
let res = '';
for(let i = 0; i < s.length; i++){
// for special characters
if(s[i].toLowerCase() === s[i].toUpperCase()){
res += s[i];
continue;
};
if(s[i] > 'm'){
res += 1;
}else{
res += 0;
};
};
return res;
};
console.log(stringToBinary(str));输出结果
以下是控制台中的输出-
00001 111000 011 010 111
热门推荐
10 新春进门祝福语大全简短
11 51祝福语毕业文案简短
12 男童毕业蛋糕祝福语简短
13 回国了祝福语简短英语
14 道士结婚文案祝福语简短
15 信件祝福语简短情话短句
16 中秋送健康祝福语简短
17 横幅家庭生日祝福语简短
18 元宵贺卡祝福语结婚简短