- Notifications
You must be signed in to change notification settings - Fork0
License
tracr/javascript-arithmetic-lab-js-intro-000
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
At this point, we can fix the firstfour broken tests: we can define functionsadd()
,subtract()
,multiply()
,divide()
inindex.js
.
Additionally, we can increment (++
) and decrement (--
) a number if it's assigned to a variable:
varnumber=5number++// 5... hmmmmnumber// 6 -- the number was incremented after it was evaluatednumber--// 6number// 5
We can also put the incrementor and decrementor operations before the number:
--number// 4++number// 5
But generally, you will see them placedafter the number (and we recommend that that's where you put them). If you're interested in the difference, take a lookhere
And, while we're on the subject, you'll usually only want to use these incrementors and decrementors when the shorthand makes what you're writing easier to read (more on whenexactly later). Instead, it's best to use the basic arithmetic operators combined with=
. For the examples below, assume thatnumber
is equal to5
(and resets for every example).
+=
modifies the value to the operator's left by adding to it the value to the operator's right:
number+=3// 8
-=
modifies the value to the operator's left by subtracting from it the value to the operator's right:
number-=2// 3
*=
modifies the value to the operator's left by multiplying it by the value to the operator's right:
number*=10// 50
/=
modifies the value to the operator's left by dividing it by the value to the operator's right:
number/=5// 1
The thing to remember about these methods is that they modify the variable in place. So if we have two functions that depend on the same external variable, they order in which they're called matters. Follow along in console:
varnumber=10functionadd5(){number+=5}functiondivideBy3(){number/=3}divideBy3()console.log(number)// 3.333333333335add5()console.log(number)// 8.333333333335// reset numbernumber=10add5()console.log(number)// 15divideBy3()console.log(number)// 5
Because these methods are more explicit, prefer+=
to++
and-=
to--
(usually).
Okay, now we're ready to write solutions for the next two functions:inc(n)
anddec(n)
.
Sometimes, we'll receive a number — well, we know it's a number, as we've seen many numbers in the past. JavaScript, however, won't know that it's a number because it shows up wrapped in quotes — JavaScript, then, thinks it's a string.
Luckily, JavaScript gives us tools to turn these strings into proper numbers (that is, numbers that JavaScript understands).
The first such tool is the functionparseInt()
, which accepts two arguments: the value to parse and the base of the value being parsed.Usually you will want to work with base 10, so a typical call toparseInt()
looks like
parseInt('2',10)// 2
What happens if we pass a representation of a non-integer toparseInt()
? Let's try it:
parseInt('2.2222',10)
If we enter the above in console, we'll see thatparseInt()
forces the parsed number to be an integer — which makes sense when we think about it, right?
What happens, though, if we pass utter nonsense toparseInt()
? Go ahead and try it in the console — something like
parseInt('nonsense!',10)
What did it return?NaN
? What is that?
NaN
stands for "not a number" — pretty handy, right? This is the number (in the JavaScript sense) that JavaScript returns when it can't determine a valid value for a numeric operation.
Above, we saw thatparseInt()
lops off everything after the decimal point and only returns integers. If we want to preserve decimals, we'll need to useparseFloat()
.
UnlikeparseInt()
,parseFloat()
accepts only a single argument, the thing to be parsed. We can use it like so:
parseFloat('80.123999')// 80.123999
You're now ready to solve the final two tests in this lab,makeInt(n)
andpreserveDecimal(n)
.
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript75.3%
- HTML24.7%