Assuming the input should be an array of unsigned integers.
"use strict";/**
* Find the pairs that have a difference of two.
*
* @param {number[]} An array of unsigned integers.
*
* @throws {Error} If the function is not called with one argument.
* @throws {TypeError} If the first argument is not an array.
* @throws {TypeError} If the first argument is not an array of integers.
* @throws {TypeError} If the first argument is not an array of unsigned integers.
*
* @return {number[][]} An array of array of unsigned integer pairs
*
* @example
* pairDifference([1, 2, 3, 4]); // [ [ 1, 3 ], [ 2, 4 ] ]
* pairDifference([4, 1, 2, 3]); // [ [ 4, 2 ], [ 1, 3 ] ]
* pairDifference([1, 23, 3, 4, 7]); // [ [ 1, 3 ] ]
* pairDifference([4, 3, 1, 5, 6]); // [ [ 4, 6 ], [ 3, 1 ], [ 3, 5 ] ]
*/functionpairDifference(numbers){if(arguments.length!==1){thrownewError("expected one argument");}if(!Array.isArray(numbers)){thrownewTypeError("expected first argument to be an array");}constlength=numbers.length;constpairs=[];for(letfirst=0;first<length;first++){constfirstNumber=numbers[first];if(!Number.isInteger(firstNumber)){thrownewTypeError("expected first argument to be an array of integers");}if(firstNumber<0){thrownewTypeError("expected first argument to be an array of unsigned integers");}for(letsecond=first+1;second<length;second++){constsecondNumber=numbers[second];if(!Number.isInteger(secondNumber)){thrownewTypeError("expected first argument to be an array of integers");}if(secondNumber<0){thrownewTypeError("expected first argument to be an array of unsigned integers");}if(Math.abs(firstNumber-secondNumber)===2){pairs.push(firstNumber,secondNumber);}}}returnpairs;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript
Assuming the input should be an array of unsigned integers.