I want to run a loop from 1 to 10 & want to print "divisible by 3" beside all the numbers which are divisible by 3 same for 5, print "divisible by both" beside the numbers which are divisible by 3 & 5 both as explained below.
Expected Output:-
1
2
3 'divisible by 3'
4
5 'divisible by 5'
6 'divisible by 3'
7
8
9 'divisible by 3'
10 'divisible by 5'
11
12 'divisible by 3'
13
14
15 'divisible by both'
It is pretty straight forward by using the arithmetic operator %,
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
arr.forEach(x=>{
if(x%3==0 && x%5==0){
console.log(`${x} is divisible by both`);
}else if(x%3 == 0){
console.log(`${x} is divisible by 3`);
}else if(x%5 == 0){
console.log(`${x} is divisible by 5`);
}else{
console.log(`${x}`);
}
})
,