Optical Operators 3 Js Challenge | Sololearn: Learn to code for FREE!

+3

Optical Operators 3 Js Challenge

I need some help with this challenge, I have managed to work out case 1 and case 3 on my editor, but with the Solo Learn editor... The parseInt() radix is returning 11 instead of 1 on case 1. How do I fix this? Do I need to change the radix? How do I solve case 3? Which has an input of 36? Any advice would be greatly appreciated. Volunteers have been divided into 5 groups with equal number of members. If any group has fewer than 5 people, more volunteers must be recruited to fill the positions. Write a program that takes the number of volunteers and outputs to the console how many volunteers need to be hired to have 5 equal groups. Sample Input 24 Sample Output 1 Explanation The nearest number to 24 that is multiple of 5 is 25, so we need 1 more volunteer (24+1=25) to get 5 equal groups.

11/9/2020 12:50:58 PM

Brandon Lee Banks

18 Answers

New Answer

+2

rest = numberVolunteers % 5; if(rest === 0){ console.log(0); } else { console.log(5 - rest); }

+3

You can use the conditional (ternary) Operators to solve this, the way the course material shows you to by using the below code: function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var remainder = numberVolunteers % 5 var pplReq = (remainder===0) ? 0: 5-remainder; console.log(pplReq) }

+1

Can we see your code ?, so we may be able to figure out why parseInt is doing that as you say and assist you further

+1

function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var volunteers = 5*5; var peopleNeeded = (volunteers % numberVolunteers); console.log(peopleNeeded); }

+1

Your welcome Brandon

+1

function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var d = numberVolunteers % 5 if (d===0) {console.log(d)} else { console.log(5-d)} } this is my code and worked perfectly by tak8ng as reference the code from JaScript but i will love to hear a good explanation

+1

well you guys use if but there is nowhere in the explenation somthing about if. i get the code it will work. but if is not explaind whit in the javescrip corse.

+1

function main() { var numberVolunteers = parseInt(readLine(), 10); // Your code here var rest = numberVolunteers % 5; var needed = 5-rest; var final = (rest==0) ? "0": needed; console.log(final); }

+1

function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here numberVolunteers= numberVolunteers%5 numberVolunteers = 5-numberVolunteers console.log(numberVolunteers) } This is my code, however, it doesnt work when numberVolunteers = 0

+1

Most answers use the 'if else' method but it hasn't been introduced yet before this problem.

+1

Peter this is the modulo operator, that means reminder of a division.

0

JaScript, thank you for the help

0

Jim did you find a way around it?

0

Here is my solution. function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var x = (numberVolunteers%5); var y = (x===0) ?"0": 5-x console.log(y) }

0

var n = prompt ("Enter the number of volunteers"); var r = n%5 ; var v = (r===0)?0:5-r ; console.log(v)

0

My solution, works perfectly function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var ifNeedMore = (numberVolunteers % 5) if (ifNeedMore>0){ console.log(5-ifNeedMore) } else { console.log("0") } }

0

So what does % meaning,did not see it in the course..

0

function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here (3 ways I am showing to solve this ) var remainder = numberVolunteers%5 ; /* solution-1 -using If -condition if(remainder%5==0){ console.log(0) } else{ y=5-remainder; console.log(y) } */ //Solution-2 Using Conditinal Ternary var requiredNumberOfpeople = (remainder===0) ? 0: 5-remainder; console.log(requiredNumberOfpeople); // solution-3 Solution in Bengali Style :P console.log(4*numberVolunteers%5) }