+ 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.

9th Nov 2020, 12:50 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
21 Respuestas
+ 3
rest = numberVolunteers % 5; if(rest === 0){ console.log(0); } else { console.log(5 - rest); }
9th Nov 2020, 5:18 PM
JaScript
JaScript - avatar
+ 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) }
14th Apr 2021, 6:27 PM
Leonard Baker
Leonard Baker - avatar
+ 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
9th Nov 2020, 2:00 PM
Abhay
Abhay - avatar
+ 1
Your welcome Brandon
9th Nov 2020, 5:32 PM
JaScript
JaScript - avatar
+ 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
5th Jan 2021, 10:54 PM
Marcos Treviño Rodriguez
Marcos Treviño Rodriguez - avatar
+ 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.
15th Jan 2021, 7:37 PM
Martijn Greven
+ 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); }
17th Mar 2021, 11:40 AM
Simone Bucci
Simone Bucci - avatar
+ 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
9th Apr 2021, 12:38 PM
UE Dikio
UE Dikio - avatar
+ 1
Most answers use the 'if else' method but it hasn't been introduced yet before this problem.
11th Apr 2021, 3:18 AM
Jim
+ 1
Peter this is the modulo operator, that means reminder of a division.
4th Nov 2021, 8:09 AM
JaScript
JaScript - avatar
0
function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var volunteers = 5*5; var peopleNeeded = (volunteers % numberVolunteers); console.log(peopleNeeded); }
9th Nov 2020, 2:09 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
JaScript, thank you for the help
9th Nov 2020, 5:27 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
Jim did you find a way around it?
9th May 2021, 6:59 AM
Cornelius Ogunjeminiyi
Cornelius Ogunjeminiyi - avatar
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) }
2nd Sep 2021, 5:44 PM
Mark Gronland
Mark Gronland - avatar
0
var n = prompt ("Enter the number of volunteers"); var r = n%5 ; var v = (r===0)?0:5-r ; console.log(v)
21st Sep 2021, 2:30 PM
Karan Chawla
Karan Chawla - avatar
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") } }
2nd Oct 2021, 1:19 AM
Liangyong Peng
Liangyong Peng - avatar
0
So what does % meaning,did not see it in the course..
3rd Nov 2021, 9:21 PM
Peter
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) }
15th Nov 2021, 10:12 AM
MD RAKIBUL HASSAN NAYON
MD RAKIBUL HASSAN NAYON - avatar
0
The solutions you give doesn’t work when the numberVolunteers is a number ending with 5, for example 25 or 35. 25%5 => rest = 0=> 5-0=5 which means the code tells us that there need to be 5 additional volunteers which is wrong.
17th Jan 2022, 11:25 PM
Sevak Rostomyan
Sevak Rostomyan - avatar
0
This works: function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var groups = 5; var rest = numberVolunteers%5; var additionalNeeded = 5-rest; additionalNeeded = (additionalNeeded == 0 || rest == 0) ? 0: additionalNeeded; console.log(additionalNeeded); }
17th Jan 2022, 11:35 PM
Sevak Rostomyan
Sevak Rostomyan - avatar