Skip to content

Latest commit

 

History

History
272 lines (212 loc) · 6.12 KB

File metadata and controls

272 lines (212 loc) · 6.12 KB

Math

Table of Contents

Downward to the nearest integer

Math.floor(1.6); //1

Upward to the nearest integer

Math.ceil(1.4); //2

Round the nearest integer

Math.round(2.4); //2
Math.round(2.5); //3

Is Integer or float?

Check 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); // true

Find a float in Array

const 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 ]

Making Digit

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

  1. Use Math.abs() to convert negative number to integer.
  2. 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)); //00043
const 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
  1. 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)); //00043

Find min/max number in Array

const array = [2, 5, 6, 9];
const max = Math.max(...array);
const min = Math.min(...array);

console.log(max); // 9
console.log(min); // 2

Finding missing number in Array

  1. Find max value.
  2. Find min value.
  3. Preparing missing[].
  4. while loop.
  5. Using indexOf() to search/find a missing value. Array.prototype.indexOf()
  6. 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 ]

Find smallest missing number in array

  1. 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));

Sum of 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); //10

Random number

Q1: 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-90

Q4: 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-100

Q5: 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-52

Q6: 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-12

Q7: 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

References: