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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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;
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!