JavaScript Challenge - Comparative Weights

This community-built FAQ covers the “Comparative Weights” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the JavaScript challenge Comparative Weights

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

function scaleOfTruthN(n) {

if (n<=1){
return 0
}
if(n <= 3 && n > 1){
return 1
}

let a = (Math.cbrt(n) - Math.floor(Math.cbrt(n)))===0? 0 : 1;
let times = Math.floor(Math.cbrt(n))+a;

return times

}

scaleOfTruthN(28);

// Leave this so we can test your code:
module.exports = scaleOfTruthN;

This was the best! Couldn’t figure out why I was only passing 4 out of 5 tests until I realised (ahem, read in a website that explains the answer) that if the number of balls is 0 or 1, 0 weighs will be required. I love that this challenge blended not only programming skills but maths problem solving skills.

My answer:

function scaleOfTruthN(n) { let i = 0; //let n = number; if (n === 1 || n === 0){ return 0; } do { if (n % 3 === 2){ n = (n + 1)/3; } else if (n % 3 === 1){ n = (n + 2)/3; } else { n = n/3; }; console.log(n); i++; } while (n > 1); return i; }; scaleOfTruthN(3); // Leave this so we can test your code: module.exports = scaleOfTruthN;

Woop!!

OMG I love this. The cube root! slaps forehead

Kudos!

i get a lot fun, I was trying with a loop before, but my loop was set not properly so I was returning more then one answer the first one was good but the rest no, and there was when I saw the cubic root + 1 was the answer, except in the numbers that have a exactly cubic root

Awesome. I had a total brain freeze. I remember asking myself “what’s the mathematical function for figuring out how many times you can divide something by 2 until you get 1?” I was googling factorials, planning to google, reverse factorial or something. Can’t believe I didn’t realise I was describing square rooting.

Anyway, we’re total bosses for completing the challenge. High five!

wouldn’t that be logarithm base 2, instead of square root?

lol, yes slaps forehead again
I’m not sure if that makes me feel better or worse

I solved by doing the log base 3, rounded up. With rounding to get rid of precision errors.

function scaleOfTruthN(n) {
  return Math.ceil(Math.round(Math.log(n) / Math.log(3) * 100) / 100);
}
function scaleOfTruthN(n) {
  let times = 0;

  while(n > 1){
    n = Math.ceil(n/3);
    ++times;
  }
  return times;
}
1 Like

2.5x slow, but with recursion :wink:

function scaleOfTruthN(n) {
  if (n === 0 || n === 1) return 0;
  if (n === 2) return 1;
  const rest = n % 3;
  const part = (n - rest) / 3;
  return 1 + scaleOfTruthN1(rest === 2 ? part + 1 : part + rest);
}
function scaleOfTruthN(n) {

  return Math.ceil( Math.log(n) / Math.log(3) );

}