Comments (9)
The other thing is if you run this straight out in the terminal with the native parseInt
function, you get 18, not 0.
parseInt('0x12');
// 18
from js-assessment.
I think that this assessment is intentionally asking that you write a brand new method, and not one that behaves exactly like the built-in parseInt
. I can totes understand how you might be confused given the name of the method in the assessment, though.
The unit tests you're referring to explain the behavior that this brand new method should have. The key difference between it and the native parseInt
is that the method you write should parse the numeric characters before any alphabetical characters in a string. That's why 12px
returns 12
, and why 0x12
returns 0
.
Does that make sense?
from js-assessment.
Not one bit at all. Could you dive in more as to why the byte 0x12
is equal to 0?
from js-assessment.
I'm only speculating here, but I'll do my best!
It's my belief that this assessment is asking us to write a method that does not behave like window.parseInt
in every situation. You wrote that the test must be wrong because parseInt('0x12');
returns a particular result. But if what I'm speculating is correct, then comparing the behavior of the method we're supposed to write to parseInt
doesn't make sense, because they're different things entirely!
The difference between the two lies in their treatment of hex values. While window.parseInt
has a notion of hex values, this new method should not.
In plain English, you might write the specification for this new method, as it relates to parseInt
, as:
In every situation, it should behave like
parseInt
, unless a hex literal string is passed in. In that situation, thex
and every character after it should be dropped.
Another specification for this new method that we're being asked to write, which doesn't make a reference to window.parseInt
, would be:
Write a method that returns a numeric value for each string. If an alphanumeric string is passed in, only parse the numeric characters that appear before the first alphabetical character.
My reasoning for thinking this is because if we changed the tests to be 12
, then the answer would just be what's already in the test:
{
parseInt: function(num) {
return window.parseInt(num);
}
}
but that's just a wrapper for a native method, which isn't anything worth writing or testing, you know?
from js-assessment.
I just think the description didn't make it clear that they wanted this parseInt
to only work with base ten and that the test was to ensure that you're specifying the radix because all the tests pass with.
{
parseInt: function(num) {
return window.parseInt(num, 10);
}
}
The default parseInt
uses base ten, unless it finds the 0x
or O
prefixes then it jumps to hex and octal, whereas most other languages will raise an error if you give it a hex or octal without a radix.
from js-assessment.
I agree. Want to make a PR to clear that up for future developers?
from js-assessment.
As @jmeas described, the intent here is to write a function that makes the test pass -- which means to not strictly follow the behavior of window.parseInt
. A PR that clarifies this would be great.
from js-assessment.
@rmurphey Giving people the explanation why it should be zero will go a long way. I'll make PR for this.
from js-assessment.
Thanks, @wldcordeiro !
from js-assessment.
Related Issues (20)
- minify browser deps? HOT 3
- random number generation test HOT 1
- Tests not loading in browser HOT 3
- Include Advanced Extra-Credit tests HOT 13
- contributors guide HOT 7
- js-koans: in-browser experience HOT 6
- async test description HOT 4
- error when I npm start HOT 2
- Best practices: declare functions safely passes by default on Firefox HOT 1
- test description HOT 1
- Replace jshint with eslint
- Command "npm test" filed HOT 2
- Possible Error in 1st Strings Exercise HOT 1
- Jade has been renamed to Pug, please install the latest version of Pug instead of using Jade HOT 1
- Comments stating desired return would drastically improve later exercises HOT 2
- in second task (cancel counting)
- Array (4th test) HOT 1
- Any plans on upgrading this repo? HOT 2
- issue with array duplicate elements HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-assessment.