Skip to content

[js] 第31天 写一个方法把0和1互转(0置1,1置0) #112

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第31天 写一个方法把0和1互转(0置1,1置0)

Activity

alery123456

alery123456 commented on May 17, 2019

@alery123456

二进制的话就可以异或了吧

undefinedYu

undefinedYu commented on May 17, 2019

@undefinedYu
Contributor

var a;
!a && 1 || 0 ;
~a+2

ghost

ghost commented on May 17, 2019

@ghost

function interchange(x) {
return +!x;
}

qq674785876

qq674785876 commented on May 20, 2019

@qq674785876

~x + 2

tzjoke

tzjoke commented on May 28, 2019

@tzjoke

如果没其他更深层次的意思的话,,,

function foo (val) {
  return val === 1 ? 0 : 1
}
myprelude

myprelude commented on Jun 13, 2019

@myprelude
function calc (val) {
  return val === 1 ? 0 : 1
}
let str = "0101"  
// =>
str = "1010"
function change(str){
 return ~str
}

这个问题不清楚考察什么 ?

Damon99999

Damon99999 commented on Jun 21, 2019

@Damon99999

var a;
!a && 1 || 0 ;
~a+2

有问题

chenyouf1996

chenyouf1996 commented on Jul 19, 2019

@chenyouf1996

不考虑浮点数的话
function change(val) {
if (typeof val === 'number') {
val += ''
let newStrArr = val.split('').map((item, index) => {
return item == '1' ? '0' : '1'
})
let newStr = newStrArr.join('')
return parseInt(newStr)
}
return val.split('').map((item, index) => item == '1' ? '0' : '1').join('')
}
let str='10101'
console.log(change(str)) //01010
str=10101
console.log(change(str)) //1010

Lucenova

Lucenova commented on Jul 25, 2019

@Lucenova

应该是考察异或运算把,记得以前学过 1^num就可以完成,但是这仅仅限于num为数字的情况

FFairy

FFairy commented on Jul 25, 2019

@FFairy

function foo (val) {
var arr = val.split("");
var a = "";
for(let i=0;i<arr.length;i++){
if( arr[i] == 1){
a += 0;
}else if(arr[i] == 0){
a += 1;
}else{
a += arr[i]
}
}
console.log(a);
}
foo("48522220000333331111")

Vi-jay

Vi-jay commented on Jul 29, 2019

@Vi-jay
const reverse = num=>+!num
const reverseNum = str => str.replace(/\d/g, m => +!+m);
hc951221

hc951221 commented on Aug 7, 2019

@hc951221

function six(str) {
let arr = str.split('')
let brr = arr.map(item => {
if (item !== '0' && item !== '1') {
return item
} else if (item === '0') {
return 1
} else if (item === '1') {
return 0
} else if (item === ' ') {
return ' '
}
})
console.log(brr.join(''))
}
six(0110..32..1) // 1001..32..0

Konata9

Konata9 commented on Aug 25, 2019

@Konata9

感觉是二进制取反的操作,可以使用 ~ 符号进行取反。

~1; // -2
~-2; // 1

如果只是单纯的 0 和 1 互换的话。

function switchOneZero(a) {
  const str = `${a}`;
  return str
    .split("")
    .map(s => (s === "0" ? 1 : s === "1" ? 0 : s))
    .join("");
}

console.log(switchOneZero(2310110));
zhuang13

zhuang13 commented on Sep 13, 2019

@zhuang13

直接异或操作就行了

const convert = num => num^1;
convert(0); // 1
convert(1); // 0
ZindexYG

ZindexYG commented on Sep 18, 2019

@ZindexYG
function interChange (num) {
  return ~num+2
}
console.log('interChange',interChange(1))
15190408121

15190408121 commented on Sep 21, 2019

@15190408121
function foo (val) {
  return val === 1 ? 0 : 1
}
qiqingfu

qiqingfu commented on Jun 22, 2020

@qiqingfu
function foo(n) {
    return -(n + 1) + 2
}

~x + 2 一样

blueRoach

blueRoach commented on Jun 30, 2020

@blueRoach
function change12(number){
  if(!typeof number){
    new Error('not number')
  }
  return number ? 0 : 1
}
smile-2008

smile-2008 commented on Oct 9, 2020

@smile-2008

function interchange(x) {
return +!x;
}

followfire

followfire commented on Oct 12, 2020

@followfire

reverse = (x: number) => x^1

YU-zhao-jun

YU-zhao-jun commented on Nov 2, 2020

@YU-zhao-jun

function test(str){
let length =str.length
let i = 0
let arr = []
const newStr = str.toString()
while(length>0){
if(!!+newStr[i]){
arr.push(0)
}else{
arr.push(1)
}
length--;
i++;
}
return arr
}

zxcdsaqwe123

zxcdsaqwe123 commented on Oct 20, 2021

@zxcdsaqwe123

//假设为字符串的1 0转换,数字的话使用toString就可以转为字符串,然后再外面套个parseInt就可以转会number类型
function changeNum(str) {
let arr = str.split('').map((x) => {
if (x === '0') {
return '1'
} else if (x === '1') {
return '0'
} else {
return x
}
})
let newstr = arr.reduce((x, y) => {
return x+y
})
return newstr
}

console.log(changeNum('1100011qwezx10'))
console.log('1100011qwezx10')

YangWenLong123

YangWenLong123 commented on Nov 11, 2021

@YangWenLong123

let a = 1;

//一般方法
if(a) {
a = 0;
} else {
a = 1;
}

//三木运算符
a = a ? 0 : 1;

//位运算
a ^= 1; --> 0

github-cxtan

github-cxtan commented on Feb 23, 2022

@github-cxtan

console.log(!0 ? 1 : 0);
console.log(~0 + 2);

github-cxtan

github-cxtan commented on Feb 23, 2022

@github-cxtan

console.log(!0 ? 1 : 0);
console.log(~0 + 2);

xiaoqiangz

xiaoqiangz commented on May 30, 2022

@xiaoqiangz

mark一下

PaleBlue0601

PaleBlue0601 commented on Sep 20, 2022

@PaleBlue0601
function conversion(str) {
  const resArr = [...str].map((ch) => {
    return  ch === '0' 
      ? '1' 
      : ch === '1' 
      ? '0' 
      : ch;
  })
  return resArr.join('');
}
wangyuanjian

wangyuanjian commented on Apr 6, 2023

@wangyuanjian

我看了半天都没看到我经常用的方法

用1减去那个数就行了!

1 = 1 - 0
0 = 1 - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@Konata9@xiaoqiangz@zhuang13

        Issue actions

          [js] 第31天 写一个方法把0和1互转(0置1,1置0) · Issue #112 · haizlin/fe-interview