Please creat a javascript file following the introduction blew,thank you so much
ID: 3746079 • Letter: P
Question
Please creat a javascript file following the introduction blew,thank you so much.
"
Implement the following function.
function sumCongruentModulo(inputArray, divisor, remainder) {
// Your code goes here
}
console.log(sumCongruentModulo([1,2,3,6], 3, 0)); // Should log 9
// Add more test cases, as I'll be running more than just the above
Any two numbers are considered congruent modulo if their remainder is the same given a specified divisor.
Given the example test case above, [1, 2, 3, 6] is the input array, 3 is the divisor, and 0 is the expected remainder. When the function is working properly, the output should be 9.
In order to sum all the congruent modulo numbers in the input array, you will need to apply the modulo operation to each input number given the provided divisor and the remainder to determine if it should be summed or not."
Explanation / Answer
function sumCongruentModulo(inputArray, divisor, remainder) {
for(i = 0; i<inputArray.length; i++) // looping through array
for(j = 0; j<inputArray.length; j++)
// When both the numbers are producing the remainder given when divided by the divisor
if(i!=j && inputArray[i] % divisor == remainder && inputArray[j] % divisor == remainder )
return inputArray[i]+inputArray[j];
}
console.log(sumCongruentModulo([1,2,3,6], 3, 0));
console.log(sumCongruentModulo([1,2,3,6], 2, 0));
console.log(sumCongruentModulo([1,2,3,6], 2, 1));
// Sample Output: 9 8 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.