permalink |
---|
/PS/P9_CalculatorJS/README.html |
Concepts used in this practical:
-
isNaN()
:isNaN()
stands for "is Not-a-Number." It's a JavaScript function that determines whether the value provided is not a valid number. It returnstrue
if the value is not a number, andfalse
if it is a number. Keep in mind thatisNaN()
can be a bit tricky because it coerces non-numeric values to numbers before performing the check. This means that values like strings that can be converted to numbers (e.g.,"123"
) might return unexpected results.Example:
isNaN("hello"); // true isNaN(123); // false isNaN("123"); // false
-
innerText
:innerText
is a property of a DOM element in the browser. It's used to get or set the visible text content of an element, excluding any hidden or styled elements. It retrieves the text content as a string, and you can use it to change the text displayed within an HTML element.Example:
<div id="myDiv">Hello, <span>world</span>!</div>
const myDiv = document.getElementById("myDiv"); const textContent = myDiv.innerText; // Gets the visible text: "Hello, world!" myDiv.innerText = "New content"; // Sets the text: "New content"
-
parseInt()
andparseFloat()
: These are JavaScript functions used to parse strings into numbers.parseInt()
parses a string and returns an integer, whileparseFloat()
parses a string and returns a floating-point number. They both extract as much numeric content as they can from the beginning of the string.Example:
const intNumber = parseInt("42"); // 42 (as an integer) const floatNumber = parseFloat("3.14"); // 3.14 (as a floating-point number)
-
querySelectorAll()
:querySelectorAll()
is a method provided by the Document Object Model (DOM) in web browsers. It's used to select multiple elements from the DOM that match a specified CSS selector. It returns a NodeList containing all matching elements.Example:
<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
const listItems = document.querySelectorAll("li"); console.log(listItems.length); // Outputs: 3