在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:learn-co-students/javascript-arithmetic-lab-bootcamp-prep-000开源软件地址:https://github.com/learn-co-students/javascript-arithmetic-lab-bootcamp-prep-000开源编程语言:JavaScript 76.1%开源软件介绍:JavaScript Arithmetic LabObjectives
IntroductionIn this lab, we're going to practice writing functions and manipulating numbers
in 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, you
say? It's a symbol that operates on one or more (usually two) objects — As you read through this lesson, you're going to be adding your solutions to
Basic MathThe most fundamental math operations work as one might expect in JavaScript: 1 + 80 // 81
60 - 40 // 20
2 * 3.4 // 6.8 (there's that floating-point arithmetic again...)
5.0 / 2.5 // 2 At this point, we can fix the first four failing tests: we can define
functions Math + AssignmentAdditionally, we can increment ( var number = 5
number++ // 5... hmmmm
number // 6 -- the number was incremented after it was evaluated
number-- // 6
number // 5 We can also put the incrementor and decrementor operations before the number: --number // 4
++number // 5 But generally, you will see them placed after the number (and we recommend that that's where you put them). If you're interested in the difference, take a look here 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 when exactly later). Instead, it's best to use the
basic arithmetic operators combined with
number += 3 // 8
number -= 2 // 3
number *= 10 // 50
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, the order in which they are called matters. Follow along in console copying each function and statement below one at a time: var number = 10
function add5() {
number += 5
}
function divideBy3() {
number /= 3
}
divideBy3()
console.log(number) // 3.333333333335
add5()
console.log(number) // 8.333333333335
// reset number
number = 10
add5()
console.log(number) // 15
divideBy3()
console.log(number) // 5 Because these methods are more explicit, we prefer Okay, now we're ready to write solutions for the next two functions:
Parsing NumbersSometimes, 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).
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论