Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

rbooker/javascript-arithmetic-lab-js-intro-000

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Objectives

  • Practice doing math with JavaScript
  • Practice writing functions that do things with numbers
  • Practice parsing strings as numbers

Introduction

In this lab, we're going to practice writing functions and manipulating numbersin JavaScript. First, though, we need to go over some basic math. In this lab,we're going to learn about various arithmetic operators. What's an operator, yousay? It's a symbol thatoperates on one or more (usually two) objects —+ isa good example. The+ operator says "add what's to the left of+ and what'sto the right of+ together."

As you read through this lesson, you're going to be adding your solutions toindex.js. You'll write a total of eight functions; use the results of runninglearn test in your IDE to guide you towards the right function names andfunctionality.

Basic Math

The most fundamental math operations work as one might expect in #"auto" data-snippet-clipboard-copy-content="1 + 80 // 8160 - 40 // 202 * 3.4 // 6.8 (there's that floating-point arithmetic again...)5.0 / 2.5 // 2">

1+80// 8160-40// 202*3.4// 6.8 (there's that floating-point arithmetic again...)5.0/2.5// 2

At this point, we can fix the firstfour failing tests: we can definefunctionsadd(),subtract(),multiply(),divide() inindex.js.

Math + Assignment

Additionally, we can increment (++) and decrement (--) a number if it'sassigned 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 recommendthat that's where you put them). If you're interested in the difference, take alookhere

And, while we're on the subject, you'll usually only want to use theseincrementors and decrementors when the shorthand makes what you're writingeasier to read (more on whenexactly later). Instead, it's best to use thebasic arithmetic operators combined with=. For the examples below, assumethatnumber is equal to5 (and resets for every example).

  • += modifies the value to the operator's left by adding to it the value tothe operator's right:
number+=3// 8
  • -= modifies the value to the operator's left by subtracting from it thevalue to the operator's right:
number-=2// 3
  • *= modifies the value to the operator's left by multiplying it by the valueto the operator's right:
number*=10// 50
  • /= modifies the value to the operator's left by dividing it by the value tothe operator's right:
number/=5// 1

The thing to remember about these methods is that they modify the variable inplace. So, if we have two functions that depend on the same external variable,the order in which they are called matters. Follow along in console copying eachfunction and statement below one at a time:

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, we prefer+= to++ and-= to-- (usually).

Okay, now we're ready to write solutions for the next two functions:increment(n) anddecrement(n). These methods should take in a number, andeither increments the provided value by one or decrements it by onerespectively, returning the result.

Parsing Numbers

Sometimes, we'll receive a number — well, we know it's a number, as we've seenmany numbers in the past. JavaScript, however, won't know that it's a numberbecause 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).

parseInt()

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 willwant 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'stry it:

parseInt('2.2222',10)

If we enter the above in console, we will see thatparseInt() forces the parsednumber to be an integer — which makes sense when we think about it, right?

What happens, though, if we pass utter nonsense toparseInt()? Go ahead andtry 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 (inthe JavaScript sense) that JavaScript returns when it can't determine a validvalue for a numeric operation.

parseFloat()

Above, we saw thatparseInt() lops off everything after the decimal point andonly returns integers. If we want to preserve decimals, we'll need to useparseFloat().

UnlikeparseInt(),parseFloat() accepts only a single argument, the thing tobe 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(string) andpreserveDecimal(string).makeInt(string) should take in a string, parse it into anbase 10 integer and return it.preserveDecimal(string) should take in a string, parse itinto a float and return it.

Resources

ViewJavaScript Arithmetic Lab on Learn.co and start learning to code for free.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript78.4%
  • HTML21.6%

[8]ページ先頭

©2009-2025 Movatter.jp