1.6 KiB
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
587d7b7e367417b2b2512b22 | Usare la funzione ParseInt con una base | 1 | https://scrimba.com/c/c6K4Kh3 | 301182 | use-the-parseint-function-with-a-radix |
--description--
La funzione parseInt()
analizza una stringa e restituisce un numero intero. Ci vuole un secondo argomento "radix", che specifica la base del numero presente nella stringa. La base può essere un numero intero compreso tra 2 e 36.
La chiamata alla funzione apparirà così:
parseInt(string, radix);
Ecco un esempio:
const a = parseInt("11", 2);
La variabile radix dice che 11
è nel sistema binario, o in base 2. Questo esempio converte la stringa 11
in un intero 3
.
--instructions--
Usa parseInt()
nella funzione convertToInteger
in modo che converta un numero binario in un intero e lo restituisca.
--hints--
convertToInteger
dovrebbe utilizzare la funzione parseInt()
assert(/parseInt/g.test(code));
convertToInteger("10011")
dovrebbe restituire un numero
assert(typeof convertToInteger('10011') === 'number');
convertToInteger("10011")
dovrebbe restituire 19
assert(convertToInteger('10011') === 19);
convertToInteger("111001")
dovrebbe restituire 57
assert(convertToInteger('111001') === 57);
convertToInteger("JamesBond")
dovrebbe restituire NaN
assert.isNaN(convertToInteger('JamesBond'));
--seed--
--seed-contents--
function convertToInteger(str) {
}
convertToInteger("10011");
--solutions--
function convertToInteger(str) {
return parseInt(str, 2);
}