How to calculate the next multiple of 4 in Node.js

2 Answers

0 votes
function next_multiple_of_4(num) { 
    return (num % 4 === 0) ? num + 4 : (num + 3) & ~0x03; 
} 
 
let nums = [25, 20, 0, -9]; 
 
nums.forEach(num => { 
    console.log(next_multiple_of_4(num)); 
});



/*
run:

28
24
4
-8

*/

 



answered Nov 21, 2024 by avibootz
0 votes
function next_multiple_of_4(num) { 
    return num + (4 - num % 4)
} 
 
let nums = [25, 20, 0, -9]; 
 
nums.forEach(num => { 
    console.log(next_multiple_of_4(num)); 
});



/*
run:

28
24
4
-4

*/

 



answered Nov 21, 2024 by avibootz

Related questions

1 answer 80 views
2 answers 108 views
1 answer 113 views
1 answer 125 views
...