You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JavaScript-Quick-Reference.md
+8-7
Original file line number
Diff line number
Diff line change
@@ -1007,10 +1007,11 @@ A floating-point number includes decimal parts, allowing for the representation
1007
1007
1008
1008
### parseInt(string, radix)
1009
1009
1010
-
The `parseInt()` function in JavaScript is used to convert a string to an integer (whole number). It takes two parameters:
1011
-
- The string to be converted (which should represent a number)
1010
+
The `parseInt()` function in JavaScript is used to convert a string to an integer (whole number). It takes two parameters:
1011
+
1012
+
- The string to be converted (which should represent a number)
1012
1013
- The base (or radix) of the numerical system to which the string belongs
1013
-
1014
+
1014
1015
When using `parseInt()`, it's crucial to specify the radix to ensure accurate conversion.
1015
1016
1016
1017
- For decimal numbers (the usual number system), use base 10.
@@ -1114,7 +1115,6 @@ Essential guide to JavaScript's Math object, covering basic constants and mathem
1114
1115
1115
1116
-`Math.random()` is a JavaScript method that generates a floating-point, pseudo-random number in the range from 0 (inclusive) to 1 (exclusive). This means it can return a number as small as 0 but never exactly 1.
1116
1117
1117
-
1118
1118
```javascript
1119
1119
// Function to get a random choice for the computer
The function getComputerChoice() utilizes Math.random() to simulate making a random choice between 'rock', 'paper', and 'scissors'. By comparing the random number against fractional thresholds, it assigns one of these three choices. This is a straightforward use case where Math.random() directly influences the flow of the program based on random selection.
1138
1138
1139
-
1140
1139
### Math.trunc
1141
1140
1142
1141
Math.trunc is a method in JavaScript's Math object that is used to remove the decimal part of a number, effectively truncating it to an integer. This method does not round the number; it simply cuts off the decimal portion.
- Math.trunc can be paired with Math.random() for generating zero-based index values, useful in selecting random elements from an array. For example, Math.trunc(Math.random() * array.length) can randomly index into an array, ensuring the index starts at 0 and is within the array bounds.
2185
+
2186
+
- Math.trunc can be paired with Math.random() for generating zero-based index values, useful in selecting random elements from an array. For example, Math.trunc(Math.random() \* array.length) can randomly index into an array, ensuring the index starts at 0 and is within the array bounds.
2187
2187
2188
2188
### Math.floor
2189
-
- Math.floor, on the other hand, can be utilized with Math.random() to create inclusive upper-bound random integers. For instance, Math.floor(Math.random() * (max - min + 1)) + min generates a random integer between min and max, inclusively, catering to situations where the starting integer is 1 and the upper limit must be part of the outcome range.
2189
+
2190
+
- Math.floor, on the other hand, can be utilized with Math.random() to create inclusive upper-bound random integers. For instance, Math.floor(Math.random() \* (max - min + 1)) + min generates a random integer between min and max, inclusively, catering to situations where the starting integer is 1 and the upper limit must be part of the outcome range.
0 commit comments