What is the correct fix?
function half(s) {
return parseInt(s) / 2;
}
half('08');
- Pass the radix:
parseInt(s, 10) - Pass the radix:
parseInt(s, 2) - Swap
parseIntforparseFloat(s) - Wrap the whole call in
String(s)
Always give parseInt a radix; without it the base is implementation-dependent for odd input.
Play Problem Solving ▸