0

Logical or Boolean 3 JS question

I’m stuck on this part of the lesson where the question is as follows along with my code. I always end up with the wrong amount. 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. function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var group = (numberVolunteers % 5) console.log(group) } If the #of volunteers is 14 I get the answer of 4 not 1 i know why it’s 4 just not 1.

10th Dec 2020, 8:21 PM
Pokemon PTA
Pokemon PTA - avatar
10 Respostas
+ 5
function main() { var numberVolunteers = parseInt(readLine(), 10) let filledvol = numberVolunteers % 5; var result = (filledvol != 0) ? 5 - filledvol : filledvol; console.log(result) }
3rd Sep 2021, 6:55 PM
IbrahimCPS
IbrahimCPS - avatar
+ 3
function main() { var numberVolunteers = parseInt(readLine(), 10) let r =numberVolunteers%5; if (r!=0) { console.log(5-r); } else { console.log(r); } } //there are 5 possible results out of "r".r= 0 or 1 or 2 or 3 or 4 so for 1 2 3 and 4 u need 5 - r to find out how many more volunteers needed but when u get 0 it means u don't need more volunteers that's why just print "r" in the else statement.
8th Jan 2021, 1:21 AM
#Mateusz
#Mateusz - avatar
+ 2
Then I think it just need find input%5 and then subtract from 5 kelly Hester In case if input%5 !=0 => 5 - input%5 If input%5 is 0, then output is 0 I think. Hope it helps.
10th Dec 2020, 9:14 PM
Jayakrishna 🇼🇳
+ 1
I just solved it without needing "if/else" commands. Like this if the remainder is > 0 you have to subtract it from 5 (that is our "perfect" number); If it is 0 it just gives you the remainder (that is actually 0) function main() { var numberVolunteers = parseInt(readLine(), 10) // Your code here var resto = (numberVolunteers % 5) > 0 ? 5- (numberVolunteers % 5) : numberVolunteers % 5 console.log(resto) }
7th Apr 2021, 8:03 AM
Andrea Giustino
Andrea Giustino - avatar
0
What is the doubt here actually? Sry Iam not understanding.... Edit : I understood now, for 14, according to description 1 more needed, so 5 - 14%5 = 1
10th Dec 2020, 8:37 PM
Jayakrishna 🇼🇳
0
Can someone please help me understand how this problem is solved. Im not sure if...else statements should be used yet as they havent been talked about in the modules. This problems relates to booleans and logic operators. Im unsure how to write the code to solve the problem
20th Mar 2021, 6:12 PM
Leanne Smith
Leanne Smith - avatar
0
So I was thinking addition for this one but it's all about subtraction!
30th Jul 2021, 9:46 AM
Can't Sing! Can't Play! Can't Write? My Music
Can't Sing! Can't Play! Can't Write? My Music - avatar
0
God that took way too long. I accidentally skipped the conditional (ternary) operator bc I did scroll down lol. function main() { var numberVolunteers = parseInt(readLine(), 10) var modulusOp = numberVolunteers % 5 var greater = (modulusOp > 0) ? (5 - modulusOp) : "0" console.log(greater) }
13th Jan 2022, 5:53 AM
Future
- 1
thats how the code should be used to figure the question out and post the correct answer. im supposed to find the number that would make the groups devisable by 5 but i cant figure out how to get my code to do it also sorry the first question should have been a 14 not 15
10th Dec 2020, 9:10 PM
Pokemon PTA
Pokemon PTA - avatar
- 2
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.
10th Dec 2020, 9:09 PM
Pokemon PTA
Pokemon PTA - avatar