Home  >  Article  >  Web Front-end  >  JavaScript program to find a triple such that the sum of the two equals the third element

JavaScript program to find a triple such that the sum of the two equals the third element

王林
王林forward
2023-09-12 12:33:031382browse

JavaScript 程序查找一个三元组,使得两个之和等于第三个元素

We will write a JavaScript program to find a triple in which the sum of two elements is equal to the third element. The program will be implemented using arrays and loop structures. We will iterate over the array and check for each element if the sum of the two elements is equal to the current element. If we find such a triple, we will return it immediately. This program will help with various mathematical calculations where we need to find triples that follow specific rules.

method

This is a way to use JavaScript to solve the problem of finding a triple such that the sum of two elements is equal to the third element in the array -

  • Loop through the array and for each element, subtract it from the sum of all other elements in the array.

  • Check if the difference obtained in step 1 exists in the array.

  • Returns a triple if a difference is found in the array.

  • Repeat steps 1 to 3 for each element in the array.

  • If no such triplet is found, return the appropriate message

Example

This is a complete JavaScript program for finding triples such that the sum of two elements is equal to the third element -

function findTriplet(arr) {
   for (let i = 0; i < arr.length; i++) {
      for (let j = i + 1; j < arr.length; j++) {
         for (let k = j + 1; k < arr.length; k++) {
            if (arr[i] + arr[j] === arr[k]) {
               return [arr[i], arr[j], arr[k]];
            }
         }
      }
   }
   return "No such triplet found";
}
let arr = [1, 4, 45, 6, 10, 8];
let result = findTriplet(arr);
console.log(result);

illustrate

  • findTriplet The function accepts an array as input and returns a triplet if the sum of two elements is equal to the third element.

  • This function uses three nested loops to check every possible combination of three elements in the array.

  • The outermost loop i iterates through each element of the array.

  • The second loop j starts from the next element of i and iterates through the remaining elements of the array.

  • The third loop k starts from the next element of j and iterates through the remaining elements of the array.

  • For each combination of the three elements arr[i], arr[j], and arr[k], this function checks whether arr[i] arr[j ] === arr[k]. If this condition is true, return the triplet [arr[i], arr[j], arr[k]].

  • If no such triplet is found, the function will return the string "No such Triplet found".

  • The program declares an array arr and calls the findTriplet function, passing arr as a parameter.

    The result of the
  • function is stored in the result variable and logged to the console.

The above is the detailed content of JavaScript program to find a triple such that the sum of the two equals the third element. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete