fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[js] 第815天 写一个方法将把true和false转为1和0

Open haizhilin2013 opened this issue 4 years ago • 7 comments

第815天 写一个方法将把true和false转为1和0

3+1官网

我也要出题

haizhilin2013 avatar Jul 08 '21 20:07 haizhilin2013

 Number(true)
function(bool){
return  bool ? 1:0
}

wj0990 avatar Jul 09 '21 00:07 wj0990

+true     // 1
+false    // 0

wheatup avatar Jul 09 '21 00:07 wheatup

const toNumber= (res)=>+res;

Jack-rainbow avatar Jul 09 '21 02:07 Jack-rainbow

function fn(bol) { let bol2 = [] if (typeof bol === "boolean") { return Number(bol) }else{ bol.map((item,index)=>{ if (bol[index]) { bol2.push(1) }else{ bol2.push(0) } }) } return bol2 }

AHaoDaDaDa avatar Jul 09 '21 07:07 AHaoDaDaDa

Number(true)//1 Number(false)//0

hhhxhf avatar Jul 14 '21 01:07 hhhxhf

function convert(params){ if(typeof params == "boolean"){ if(params == true || params == false){ console.log(Number(params)) } } } convert(false)

sunshine789-bot avatar Jul 15 '21 08:07 sunshine789-bot

Number

WangXi01 avatar Jul 22 '22 05:07 WangXi01