Skip to content

Instantly share code, notes, and snippets.

@entity1991
Created February 26, 2020 21:50
Show Gist options
  • Save entity1991/d3013160ec7122b35f657b88d3515ebe to your computer and use it in GitHub Desktop.
Save entity1991/d3013160ec7122b35f657b88d3515ebe to your computer and use it in GitHub Desktop.
JS tasks
function sum(from, to) {
let total = 0
for (let i = from; i <= to; i += 1) {
total += i
}
return total
}
function findOddNumbers(from, to) {
let oddNumbers = []
for (let i = from; i <= to; i += 1) {
if (i % 2 === 0) {
oddNumbers.push(i)
}
}
return oddNumbers
}
function comparator(a, b) {
if (a === b) return 0
return a < b ? -1 : 1
}
function factorial(number) {
let f = 1
for(let i = 1; i <= number; i++) {
f = f * i
}
return f
}
function allFactorials(number) {
let factorials = []
for(let i = 1; i <= number; i++) {
factorials.push(factorial(i))
}
return factorials
}
function numberify() {
let stringNumber = ''
for(let i = 0; i < arguments.length; i++) {
stringNumber += arguments[i].toString()
}
return parseInt(stringNumber)
}
const square = (a, b) => b ? a * b : a * a
function isNumberPerfect(number) {
let sum = 0
for(let i = 1; i < number; i++) {
if (number % i === 0) {
sum += i
}
}
return sum === number
}
function findPerfectNumbers(from, to) {
let numbers = []
for (let i = from; i <= to; i ++) {
if(isNumberPerfect(i)) {
numbers.push(i)
}
}
return numbers
}
function padNumber(number) {
return number.toString().padStart(2, '0')
}
function displayTime(h, m, s) {
return `${padNumber(h)}:${padNumber(m)}:${padNumber(s)}`
}
function getSeconds(h, m, s) {
let seconds = s
let secondsInMinutes = m * 60
let secondsInHours = h * 60 * 60
return seconds + secondsInMinutes + secondsInHours
}
function getTimeFromSeconds(s) {
let hours = Math.floor(s / 60 / 60)
const secondsInHours = hours * 60 * 60
let minutes = Math.floor((s - secondsInHours) / 60)
const secondsInMinutes = minutes * 60
let seconds = s - secondsInHours - secondsInMinutes
return displayTime(hours, minutes, seconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment