A number multiplied by 3 at a time, and multiplied several times will exceed 100.

let count = 0;
function add(num){
    if(num >= 100){
        return count;
    }else{
        countPP;
        add(num*3)
    }
    return count;
}
let ADD = add;
console.log(ADD(1.2));

this is what I have written so far. I have tried to put the count declaration into add and write it with closures, but the return value in the form of closures I wrote is undefined, and why it is necessary to throw count twice. If it is only judged that the count is thrown, the return value is undefined

.

I"d like to ask you what to do if you put the declaration count into the add.

Mar.05,2021

does a math problem have to be so complicated?

function add(num)
{
    return parseInt(Math.log(100 / num) / Math.log(3)) + 1;
}

while or recursion is better.

function mult (val) {
    let count = 0
    if (val <= 0) {
        return Infinity
    }
    while (val <= 100) {
        val *= 3
        countPP
    }
    return count
}

1. You wrote it correctly. The return value is normal

.

clipboard.png

2. Just change it to this

let count = 0;
function add(num){
    if(num >= 100){
        return count;
    }else{
        countPP;
        return add(num*3)
    }
}

Why don't you just change your thinking to log3 (100max x) and round it up. Do it from the point of view of division.


you can wrap another layer of functions so that the closure is released at the end.

it is relatively simple to deal with it mathematically. The basic idea is 3 ^ n * x > 100 = > n > log3 (100max x) = > n > ln (100amp x) / ln (3) .

some boundary conditions need to be paid attention to.

function add (num) {
  if (typeof num !== 'number' || num <= 0) {
      return false
     }
     if (num >= 100) {
         return 0
     }
     const result = Math.ceil(Math.log(100 / num) / Math.log(3))
  return result <= Number.MAX_VALUE ? result : false
    }

console.assert(add(0.1) === 7, 0.1)
console.assert(add(1) === 5, 5)
console.assert(add(10) === 3, 3)
console.assert(add(99) === 1, 1)
console.assert(add(100) === 0, 100)
console.assert(add(101) === 0, 101)
console.assert(add(Number.MAX_SAFE_INTEGER) === 0, 'Number.MAX_SAFE_INTEGER')
console.assert(add(Number.MAX_SAFE_INTEGER + 1) === 0)
console.assert(add(Number.MAX_VALUE) === 0)
console.assert(add(Number.MAX_VALUE + 1) === 0)
console.assert(add(Infinity) === 0)
console.assert(add(-Infinity) === false)
console.assert(add(Number.MIN_SAFE_INTEGER) === false)
console.assert(add(Number.MIN_SAFE_INTEGER - 1) === false)
console.assert(add(Number.MIN_VALUE) === false)
console.assert(add(Number.MIN_VALUE - 1) === false, 'Number.MIN_VALUE - 1')
console.assert(add(-1) === false)
console.assert(add(0) === false)
console.assert(add(true) === false)
console.assert(add(NaN) === false)
console.assert(add('') === false)
console.assert(add({}) === false)
console.assert(add() === false)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5a423-43b47.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5a423-43b47.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?