Study Notes

Study Notes

  • Source
  • WebDesign
  • Javascript

›Intermediate Algorithm Script

Basic Javascript

  • Basic JavaScript Overview

ES6

  • ES6 Overview

Regular expression

  • Regular Expression Overview
  • Lookahead
  • Capture Group

Debugging

  • Debugging Overview

Object Oriented Programming

  • Object Oriented Programming Overview
  • Mixin
  • Closure
  • IIFE

Functional Programming

  • Functional Programming

Basic Algorithm Script

  • Reverse a String
  • Factorialize a Number
  • Find the Longest Word in a String
  • Return Largest Numbers In Arrays
  • Repeat a String
  • Truncate a String
  • Finders Keepers
  • Boo Who
  • Title Case a Sentence
  • Slice and Splice
  • Falsy Bouncer
  • javascript-algorithms-anddata-structures/basic-algorithm-scripting/where-do-i-belong
  • Mutations
  • Chunky Monkey

Intermediate Algorithm Script

  • Sum All Numbers in a Range
  • Diff Two Arrays
  • Seek and Destroy
  • Wherefor Art Thou
  • Spinal Tap Case
  • Search and Replace
  • DNA Pairing
  • Missing letters
  • Sorted Union
  • Convert HTML Entities
  • Sum All Odd Fibonacci Numbers
  • Smallest Common Multiple
  • Drop it
  • Steamroller
  • Binary Agents
  • Everything Be True
  • Arguments Optional
  • Make a Person
  • Map the Debris

Smallest Common Multiple

/**
 * 求两个数的最小公倍数
 * @param {number} num1
 * @param {number} num2
 */
function smallestCommonsOfTwoNumber (num1, num2) {
  let max = num1 < num2 ? num1 : num2

  for (let i = 2; i <= max; i++) {
    if (num1 % i === 0 && num2 % i === 0) {
      return i * smallestCommonsOfTwoNumber(num1 / i, num2 / i)
    }
  }

  return num1 * num2
}

/**
 * 求在一定范围内的所有数的最小公倍数
 * @param {array} arr
 */
function smallestCommons(arr) {
  // 现将数组的元素从小到大排序
  const copyArr = [...arr];
  copyArr.sort((a, b) => a-b);

  // 获取一定范围连续的数字的数组
  let allNumArr = [];
  for(let i = copyArr[0]; i <= copyArr[1]; i++) {
    allNumArr.push(i);
  }

  // 先取数组中的前两个数字的最小公倍数,然后再获取这个公倍数与下一个数组元素的最小公倍数
  // 如此循环计算,即可获取一定范围之内所有数字的最小公倍数
  const result = allNumArr.reduce((a, b) => smallestCommonsOfTwoNumber(a, b))

  return result;
}


smallestCommons([1,5]);
← Sum All Odd Fibonacci NumbersDrop it →
Copyright © 2019 罗惠东