0
Logical or boolean operators 3 - volunteer teams
Hi, can this challenge be solved with other code? up to this moment function IF is not mentioned in course... var numberVolunteers = parseInt(readLine(), 10); var x = numberVolunteers % 5; if (x!=0) console.log(5 - x); else console.log(x);
3 Respostas
+ 6
Yeah, you can use ternary operator instead
function main(){
var numberVolunteers = parseInt(readLine(), 10);
var x = numberVolunteers % 5;
var res = x!=0 ? 5-x:x
console.log(res)
}
https://www.sololearn.com/learn/Javascript/1133/
+ 5
When numberVolunteers = 10, x = 10%5 that is equal to 0.
Hope you know `%` modulus operator is used to get the reminder. `/` is used for division.
0
I think I need more feedback... the code works, but I don't know why...
i put 2 digits to test it:
if x = 9
var x = numberVolunteers % 5; //here x will be 4
var res = x!=0 ? 5-x:x //here will be 4 is not equal to 0 so: 5-4 = 1 and it is OK
console.log(res) }
but, when I check
x=10
var x = numberVolunteers % 5; //here x will be 2
var res = x!=0 ? 5-x:x //here will be 2 is not equal to 0 so: 5-2 = 3 and in this place i got lost...
console.log(res) }