Section 12: Numbers, Dates, Intl and Timers Flashcards
- How to extract number parts from a string?
*
*
*
* - Get result of
“console.log(Number.parseInt(‘23px’));
console.log(Number.parseInt(‘px23’));
console.log(Number.parseInt(‘ 2.5rem ‘));
console.log(Number.parseFloat(‘ 2.5rem ‘));
console.log(parseFloat(‘ 2.5rem’));
console.log(Number.parseInt('23px')); // 23 console.log(Number.parseInt('px23')); // NaN console.log(Number.parseInt(' 2.5rem ')); // 2 console.log(Number.parseFloat(' 2.5rem ')); // 2.5 console.log(parseFloat(' 2.5rem')); // 2.5"
Problem
Create a function(min number, max number). Generate random number
“const randomInt = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;”
Definition
- How many way to round integer number?
- How many way to round decimal number? Problem? How to fix?
- + Math.trunc(): Cut decimal part
+ Math.round(): Round to nearest number
+ Math.ceil(): Round up
+ Math.floor(): Round down - toFixed(). Convert result to string. Use “+” to convert back to number
Problem
Get result of
“console.log(Math.trunc(23.3));
console.log(Math.round(1.2)); //
console.log(Math.round(1.5)); //
console.log(Math.round(1.6)); //
console.log(Math.round(-1.1)); //
console.log(Math.round(-1.5)); //
console.log(Math.round(-1.6)); //
console.log(Math.ceil(1.2)); //
console.log(Math.ceil(1.5)); //
console.log(Math.ceil(1.6)); //
console.log(Math.ceil(-1.1)); //
console.log(Math.ceil(-1.5)); //
console.log(Math.ceil(-1.6)); //
console.log(Math.floor(1.2)); //
console.log(Math.floor(1.5)); //
console.log(Math.floor(1.6)); //
console.log(Math.floor(-1.1)); //
console.log(Math.floor(-1.5)); //
console.log(Math.floor(-1.6)); //
"console.log(Math.trunc(23.3)); console.log(Math.round(1.2)); // 1 console.log(Math.round(1.5)); // 2 console.log(Math.round(1.6)); // 2 console.log(Math.round(-1.1)); // -1 console.log(Math.round(-1.5)); // -1 console.log(Math.round(-1.6)); // -2 console.log(Math.ceil(1.2)); // 2 console.log(Math.ceil(1.5)); // 2 console.log(Math.ceil(1.6)); // 2 console.log(Math.ceil(-1.1)); // -1 console.log(Math.ceil(-1.5)); // -1 console.log(Math.ceil(-1.6)); // -1 console.log(Math.floor(1.2)); // 1 console.log(Math.floor(1.5)); // 1 console.log(Math.floor(1.6)); // 1 console.log(Math.floor(-1.1)); // -2 console.log(Math.floor(-1.5)); // -2 console.log(Math.floor(-1.6)); // -2"
Problem
“Problem: Round the requested loan amount
- Round down load amount
- Other amount should have 2 decimal part after “.”
-
https://codepen.io/luckily1702/pen/BavxZoV”
“Hint
- When enter request loan, automatically round down (Ex: 300.45 => 300)
- Display 2 decimal parts in 3 places: Current balance, display movements, Summary (In, out, interest) “
Problem
”/* write a isEven function
- Take a number as argument
- Return true if number is even
*/”
“const isEven = number => number % 2 === 0;
console.log(isEven(3)); // false
console.log(isEven(6)); // true
console.log(isEven(9)); // false”
Problem
/*
In ‘Bankist App’. When login, user click on label summary amount and obser color change in movements row
- Color in every second movements row in browser to orangered; (line 0,2,4,6,…)
- Color in every third movements row in browser to blue; (line 0,3,6,9)
https://codepen.io/luckily1702/pen/BavxZoV
*/
“labelBalance.addEventListener(‘click’, function () {
[…document.querySelectorAll(‘.movements__row’)].forEach((el, i) => {
if (i % 2 === 0) el.style.backgroundColor = ‘orangered ‘;
if (i % 3 === 0) el.style.backgroundColor = ‘blue ‘;
});
});”
Definition
- How to work with big number? What is that data type?
- How many way to display correct big number?
- bigint
- + use “n” at the end of number
+ use BigInt( ) (only work with smaller big number than “n” )