[英]Array.join("") method not working JavaScript
I'm confused about why array.join("")
is not working properly in this function.我很困惑为什么
array.join("")
在这个函数中不能正常工作。
If the letter is am it will show 0 and the otherwise is 1 convertBinary("house")
➞ "01110"如果字母是 am 它会显示 0 否则是 1
convertBinary("house")
➞ "01110"
function convertBinary(str) {
var jack=str.split("")
return jack.map(function(e) {
var array=[]
if(e.match(/[abcdefghijklm]/g)) {
array.push(0)
} else {
array.push(1)
}
return array.join("");
})
}
Where did I go wrong.我哪里做错了。
You need to apply the .join("")
to the result of the map()
call, instead of inside the map() inner function, which also should return just 0
or 1
for each character.您需要将
.join("")
应用于map()
调用的结果,而不是在 map() 内部函数中,它也应该为每个字符返回0
或1
。
Also it would be better to use regex.test(str)
because we are only interested in if there is a match (true or false), see eg what is the difference between .match and .test in javascript .此外,最好使用
regex.test(str)
因为我们只对是否存在匹配(真或假)感兴趣,请参阅例如javascript 中 .match 和 .test 之间的区别是什么。
Like this:像这样:
function convertBinary(str) { var jack = str.split("") return jack.map(function(e) { if (/[abcdefghijklm]/.test(e)) { return 0; } else { return 1; } }).join(""); } console.log(convertBinary("house"));
The array.join() must be outside the jack.map() function, like this: array.join() 必须在 jack.map() 函数之外,如下所示:
var jack_bin = jack.map(function(e)
{
var array=[]
if(e.match(/[abcdefghijklm]/g))
{
return '0';
}
else
{
return '1';
}
})
return jack_bin.join("");
Please try this solution请试试这个解决方案
function convertBinary(str) { const jack = str.split(''); const binArr = jack.map(e => { if (e.match(/[abcdefghijklm]/g)) { return 0; } else { return 1; } }); return binArr.join(''); } console.log(convertBinary('house'))
You could short it a bit by using a chain of methods and use test
instead of match
, because you need a boolean value, not an array of matched items/null.您可以通过使用一系列方法并使用
test
而不是match
来缩短它,因为您需要一个布尔值,而不是匹配项/null 的数组。
Then you need to take eiter 1
or 0
and join the result for getting a string.然后你需要取 eiter
1
或0
并加入结果以获得字符串。
function convertBinary(string) { return string .split("") .map(function(e) { return /[abcdefghijklm]/.test(e) ? 1: 0; }) .join(''); } console.log(convertBinary("house")) // "01110"
Initialize array and return array.join() outside the map function:初始化数组并在 map 函数外返回array.join() :
function convertBinary(str) { var jack=str.split(""); var array = jack.map(function(e){ if(e.match(/[abcdefghijklm]/g)){ return 0; } else { return 1; } }) return array.join(""); } console.log(convertBinary('house'));
The key point here is that it should be fixed as jack.map(...).join()
, because jack.map()
returns an array.这里的关键是它应该固定为
jack.map(...).join()
,因为jack.map()
返回一个数组。
function convertBinary(str) { var jack=str.split("") return jack.map(function(e) { var array=[] if(e.match(/[abcdefghijklm]/g)) { array.push(0) } else { array.push(1) } return array; }).join("") } console.log(convertBinary("house")); // output is '01110'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.