Hello I recently came across a question where an array was fed into a function along with two other parameters(low & high). The question asked to divide the numbers from low to high by each value in the array and if all values were divisible to print the number and "all_match" and if only one or some were divisible to print the number and "one_match", otherwise just print the number. So I think the solution for an array with two numbers is as follows:
function dividedByArray(data,low,high) {
for (i=low; i<=high; i++) {
if (i % data[0] === 0 && i % data[1] === 0) {
console.log(i + " all_match")
} else if (i % data[0] === 0 || i % data[1] === 0) {
console.log(i + " one_match")
} else {
console.log(i)
}
}
}
dividedByArray([2,3],5,10)
I have been trying to figure out how would you do this problem if you didn't know how long the array was (lets say the array was 4 numbers). I am relatively new to this. I have been trying out the map function but can't seem to figure out the way to do it. I also tried another for loop within the loop but it just printed everything twice. Thanks!
function dividedByArray(data,low,high) {
for (i=low; i<=high; i++) {
var passed = 0;
for(var curr in data) {
if(i % data[curr] === 0) {
passed++;
}
}
switch(passed) {
case data.length :
console.log(i + 'all_match');
break;
case 0 :
console.log(i)
break;
default:
console.log(i + " one_match");
}
}
}