2023-03-24

 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.

Solution

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
  }
}

Testing

Solution tested in REPL:

https://www.typescriptlang.org/play?#code/MYewdgzgLgBBIBsCuUCW4YF4YAoxIFsAjAUwCcIAuGAQTLIEMBPAHn2PID4AaGAa2rtSZAJTUiIRCQZgYAHxgymWTjADeAWABQMGADMQZXAhKx2MEHphDyEEeu27dJ2GRIEs-GAFprhR05uUEhksniEwhAAdKhgwMgAJiQQOG4EIvYA-DBQZEgkMNR6DAgQJAEAvtpVWtqgkFJRCCAA5jgB8Mho4DgA2gCMAAy8-QCsvADMvADsALoj0yLa9gD0Kzl55VpAA

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

This node last updated October 9, 2024 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