Table of Contents
Math.floor(1.6); //1Math.ceil(1.4); //2Math.round(2.4); //2
Math.round(2.5); //3Check for a remainder when dividing by 1. If a remainder is 0, the value is Integer. If not, it is float.
const isInt = (a) => {
const remain = a % 1;
const result = remain === 0 ? true : false;
console.log(result);
};
isInt(5.3); //false
isInt(2); //true
isInt(2.1); // false
isInt(3); // trueconst array = [3, 4.5, 2, 6.3];
const result = array.filter((value) => {
if (value % 1 > 0) {
return value;
}
});
console.log(result);
// [ 4.5, 6.3 ]Creat a function to make digits. Given two arguments (number, nth) and if number is negative, it should conver to Interger. Example: findDigit(-43,5)); //00043
- Use Math.abs() to convert negative number to integer.
- Use Math.pow() to create nth digit. Example: 5th digit: 100000
function makeDigit(number, nth) {
var tmp = 0;
tmp = Math.pow(10, nth);
tmp = tmp + Math.abs(number);
var array = tmp.toString().split("");
array.shift();
var str = array.join("");
return str;
}
console.log(makeDigit(-43, 5)); //00043const findDigit = (num, nth) => {
let digitNum = Math.abs(num) + Math.pow(10, nth);
const numArray = digitNum.toString().split("");
numArray.splice(0, 1);
const result = numArray.join("");
console.log(result);
};
findDigit(-43, 5, 5); //00043- If you don't know Math.abs() to convert negative number, it multiply by -1. Example:
-43x-1 = 43
function makeDigit2(number, nth) {
number = number * -1;
var sum = 1;
for (var i = 0; i < nth; i++) {
sum = sum * 10;
}
var output = sum + number;
var array = output.toString().split("");
array.shift();
var str = array.join("");
return str;
}
console.log(makeDigit2(-43, 5)); //00043const array = [2, 5, 6, 9];
const max = Math.max(...array);
const min = Math.min(...array);
console.log(max); // 9
console.log(min); // 2- Find max value.
- Find min value.
- Preparing missing[].
- while loop.
- Using indexOf() to search/find a missing value.
Array.prototype.indexOf() - Add a missing value in missing[].
const myArray = [5, 2, 6, 1, 3]; //4
const findMissing = (array) => {
const max = Math.max(...array);
const min = Math.min(...array);
const result = [];
for (let i = min; i < max + 1; i++) {
if (array.indexOf(i) === -1) {
result.push(i);
}
}
return result;
};
console.log(findMissing(myArray)); //[ 4 ]- Write a function:
- Given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
- For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
- Given A = [1, 2, 3], the function should return 4.
- Given A = [−1, −3], the function should return 1.
break vs return
break is used when you want to exit from the loop, while return is used to go back to the step where it was called or to stop further execution.
// const array = [1, 3, 6, 4, 1, 2]; //5
const array = [1, 2, 3]; //4
// const array = [-1, -3]; //1
const solution = (array) => {
const max = Math.max(...array);
const min = Math.min(...array);
let result = 0;
for (let i = min; i <= max; i++) {
if (array.indexOf(i) === -1) {
result = i;
break;
}
}
if (result === 0) {
result = max + 1;
} else if (result < 0) {
result = 1;
}
return result;
};
console.log(solution(array));const myArray = [3, 5, 2];
const sum = (array) => {
const result = array.reduce((acc, value) => {
acc = acc + value;
return acc;
}, 0);
console.log(result);
};
sum(myArray); //10Q1: Generate random number 0-99
Math.floor(Math.random() * 100);- Step 1 Generate random number 0-1(1 is exclosive).
- Step 2 Multiply max number that you want to + 1.
- Step 3 Downward the result using Math.floor().
Q2: Generate random number 0-50
Math.floor(Math.random() * 51);Q3: Generate random number 30-90
//Math.floor(Math.random()*91) // Step1: 0-90
//Math.floor(30+Math.random()*91) // Step2: 30-120
Math.floor(30+(Math.random()*61) // Step3: 30-90Q4: Generate random number 1-100
//Math.floor(Math.random()*101); //Step1: 0-100
//Math.floor(1+Math.random()*101); //Step2: 1-101
Math.floor(1 + Math.random() * 100); //Step 3: 1-100Q5: Random number for a deck of Card with Joker
- The deck of card is 52 + joker = 53
- Generate the number 0-52
Math.floor(Math.random() * 53); //0-52Q6: Generate random number 1-12
// Math.floor(Math.random()*13) //0-12
// Math.floor(1+Math.random()*13) //1-13
Math.floor(1 + Math.random() * 12); //1-12Q7: Generate random number 5-7
// Math.floor(Math.random()*8); //0-7
// Math.floor(5+Math.random()*8); //5-12
Math.floor(5 + Math.random() * 3); //5-7