おいーす、もりたけです。
フリーコードキャンプを日本語で解説!の第105弾です。
JavaScript Algorithms and Data Structures Certification (300 hours)の中の、
Basic JavaScriptの中の、
Use the parseInt Function with a Radix
です。
さっそく見ていきましょう。
まずは本文から。
The
parseInt()
function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.The function call looks like: parseInt(string, radix);
And here’s an example:
var a = parseInt("11", 2);
The radix variable says that “11” is in the binary system, or base 2. This example converts the string “11” to an integer 3.
Use parseInt()
in theconvertToInteger
function so it converts a binary number to an integer and returns it.
解説していきます。
今回はperseInt関数にて、基数を指定して整数を取得するという内容です。
perseInt関数は2番目の引数に基数をとります。
基数は2〜36まで指定できます。
例を見てみましょう。
次のようにかきます。
parseInt(string, radix);
var a = parseInt("11", 2);
これで、aは11の基数が2、つまり2進数の11なので整数3を返します。
では課題を見てみましょう。
関数convertToInteger
にてperseInt関数を使って2進数の数値を整数に直して返せ。という課題です。
第二引数に2を指定するだけですね。
答えはこうです。
という事で今回はperseInt関数で基数の異なる数値を整数に直すという内容でした。
では次回!