8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

js 与えられた整数を二進数で返す

Last updated at Posted at 2018-05-07

##お題

負でない整数nが与えられた場合、その数値を二進数で返す関数を書いてください。

function toBinary(n){
 //write your code.
}
toBinary(10);//1010

##使ったもの
parseInt();
toString();

##コード


toBinary = (n) => parseInt(n.toString(2));

##考え方
toString()の引数2とすることで二進数かつ文字へ変換する。//'1010'
parseInt();で二進数にした文字列を数値へ変換する。//1010
parseInt();

##他code

let toBinary = n => +n.toString(2)

他にも解答コードある方、コメントへどうぞ

8
7
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?