0030【Edabit ★☆☆☆☆☆】【布尔转字符串】Boolean to String Conversion
0030【Edabit ★☆☆☆☆☆】【布尔转字符串】Boolean to String Conversion
bit_operations
conditions
logic
strings
Instructions
Create a function that takes a boolean variable flag and returns it as a string.
Examples
boolToString(true) // "true"
boolToString(false) // "false"
Notes
- Don’t forget to return the result.
Solutions
function boolToString(flag) {
return flag & 1? "true" : "false" ;
}
TestCases
let Test = (function(){
return {
assertEquals:function(actual,expected){
if(actual !== expected){
let errorMsg = `actual is ${actual},${expected} is expected`;
throw new Error(errorMsg);
}
}
}
})();
Test.assertEquals(boolToString(true), "true")
Test.assertEquals(boolToString(false), "false")