Skip to main content
Post Undeleted by James
added 18 characters in body
Source Link
James
  • 567
  • 2
  • 8

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])

I was actually ignorant to the fact your code actually works which means it can be done in one pass :) here's a slightly neater / more concise version of what you already have:

const checkIfSumFromTwoNumbers = (arrayOfNum, targetValue) => {
  const seen = {};
  return arrayOfNum.reduce((matches, val) => {
    const delta = targetValue - val;
    seen[delta] && matches.push([delta, val]);
    seen[val] = true;
    return matches;
  }, []);
};

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])

I was actually ignorant to the fact your code actually works which means it can be done in one pass :) here's a slightly neater / more concise version of what you already have:

const checkIfSumFromTwoNumbers = (arrayOfNum, targetValue) => {
  const seen = {};
  return arrayOfNum.reduce((matches, val) => {
    const delta = targetValue - val;
    seen[delta] && matches.push([delta, val]);
    seen[val] = true;
    return matches;
  }, []);
};
added 18 characters in body
Source Link
James
  • 567
  • 2
  • 8

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])
No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])
Post Deleted by James
added 27 characters in body
Source Link
James
  • 567
  • 2
  • 8

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
   }, [])

No, each number requires a second pass to determine whether combined with an existing one it is a match - doesn't mean it can't be short though e.g.

const checkIfSumFromTwoNumbers = (arrayOfNums, targetValue) => 
  arrayOfNums.reduce((matches, val) => {
      const target = arrayOfNums.find(x => (val + x) === targetValue);
      target && matches.push([val, target]);
      return matches;
   }, [])
added 2 characters in body
Source Link
James
  • 567
  • 2
  • 8
Loading
Source Link
James
  • 567
  • 2
  • 8
Loading