Skip to content

Commit 914e899

Browse files
committed
minor content updates
1 parent 9bc87c9 commit 914e899

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

JavaScript-Quick-Reference.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ A comprehensive yet concise quick-reference and overview of JavaScript fundament
2626
- [3.6 Error Handling](#36-error-handling)
2727
## Global JavaScript Objects
2828
- [4.1 String](#41-string)
29-
- [4.2 Number](#42-number)
29+
- [4.2 Numbers](#42-numbers)
3030
- [4.3 Math](#43-math)
3131
- [4.4 Date](#44-date)
3232
## DOM & DOM Manipulation
@@ -527,6 +527,7 @@ person.greet("Hello"); // Calls the greet method with "Hello" as an argument, ou
527527
```
528528

529529
### Methods of the Object Constructor
530+
Methods of the Object constructor in JavaScript provide a set of utility functions for creating, manipulating, and working with objects, including methods for object creation, property manipulation, and property enumeration.
530531
- `Object.assign(target, ...sources)` Copies values from source to target objects.
531532
- `Object.create(proto, propertiesObject)` Creates a new object with the specified prototype and properties.
532533
- `Object.defineProperty(obj, prop, descriptor)` Defines a new property on an object.
@@ -688,6 +689,24 @@ Fundamental number-related functionalities in JavaScript.
688689
- `parseInt("10", 10);` Converts the string "10" to an integer (base 10).
689690
- `parseInt("10", 2);` Converts the string "10" to an integer (base 2, binary).
690691

692+
### parseInt Example
693+
694+
```javascript
695+
// Sample input from the user
696+
const userInput = "42";
697+
698+
// Using parseInt to convert the string to an integer
699+
const parsedNumber = parseInt(userInput);
700+
701+
// Checking if the conversion was successful
702+
if (!isNaN(parsedNumber)) {
703+
console.log(`Parsed number: ${parsedNumber}`);
704+
console.log(`Type of parsedNumber: ${typeof parsedNumber}`);
705+
} else {
706+
console.log("Conversion failed. Please enter a valid number.");
707+
}
708+
```
709+
691710
### parseFloat(string)
692711
`parseFloat("3.14");` Converts the string "3.14" to a floating-point number.
693712

@@ -723,8 +742,6 @@ console.log(morePrimes); // [7, 2, 3, 5, 11]
723742
- `Number.isSafeInteger(10);` true
724743
- `Number.MAX_VALUE;` The largest positive representable number
725744
- `Number.MIN_VALUE;` The smallest positive representable number
726-
- `Number.parseFloat("5.5");` 5.5
727-
- `Number.parseInt("10", 10);` 10
728745

729746
[🔝 Back to Top](#top)
730747
---
@@ -758,6 +775,27 @@ Essential guide to JavaScript's Math object, covering basic constants and mathem
758775

759776
### Random Number Generation
760777
- `Math.random();` Generates a random number between 0 (inclusive) and 1 (exclusive)
778+
779+
### Example of Math.random() to generate a random choice
780+
781+
```javascript
782+
// Function to get a random choice for the computer
783+
function getComputerChoice() {
784+
const randomNumber = Math.random(); // Generates a random number between 0 (inclusive) and 1 (exclusive).
785+
786+
if (randomNumber < 1/3) {
787+
return 'rock';
788+
} else if (randomNumber < 2/3) {
789+
return 'paper';
790+
} else {
791+
return 'scissors';
792+
}
793+
}
794+
795+
// Example usage:
796+
const computerSelection = getComputerChoice();
797+
console.log(`Computer chose: ${computerSelection}`);
798+
```
761799

762800
[🔝 Back to Top](#top)
763801
---
@@ -945,6 +983,14 @@ For additional checks, complex logic, or reusability, using a named function off
945983
document.getElementById("myButton").addEventListener("click", buttonClickFunction);
946984
```
947985

986+
### DOMContentLoaded Event Listener
987+
To execute code when the DOM is fully loaded and parsed, use the DOMContentLoaded event.
988+
989+
```javascript
990+
document.addEventListener("DOMContentLoaded", function() {
991+
});
992+
```
993+
948994
### Removing Event Listeners
949995
- `elementName.removeEventListener("click", buttonClickFunction);`
950996

0 commit comments

Comments
 (0)