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.
10 Respostas
+ 5
function main() {
var numberVolunteers = parseInt(readLine(), 10)
let filledvol = numberVolunteers % 5;
var result = (filledvol != 0) ? 5 - filledvol : filledvol;
console.log(result)
}
+ 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.
+ 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.
+ 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)
}
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
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
0
So I was thinking addition for this one but it's all about subtraction!
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)
}
- 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
- 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.