24 Mar 2023

Return whether any Two Numbers in an Array add up to a Given Number

Taken from DailyCodingProblem:

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.


This is the answer I came up with:

const solution = (numbers: Array<number>, k: number): boolean | any => {
  for (let num of numbers) {
    let rem = k - num
    return (numbers.includes(rem)) ? true : false


Solution tested in REPL:


Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated November 7, 2023 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems