
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Using operations to produce desired results in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of exactly 4 numbers, arr, as the first argument and a target as the second argument.
Our function need to judge whether the numbers in the array arr could operated through *, /, +, -, (, ) to get the value equal to target.
For example, if the input to the function is
Input
const arr = [5, 3, 2, 1]; const target = 4;
Output
const output = true;
Output Explanation
Because we can achieve 4 like this −
(5 - 1) * (3 - 2) = 4
Example
Following is the code −
const arr = [5, 3, 2, 1]; const target = 4; const canOperate = (arr = [], target = 1) => { const isValid = x => Math.abs(x - target) < 0.0000001 const helper = (arr = []) => { if (arr.length === 1) { return isValid(arr[0]) } let valid = false for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { const nextArr = arr.filter((x, index) => index !== i && index !== j) valid = valid || helper([...nextArr, arr[i] + arr[j]]) || helper([...nextArr, arr[i] - arr[j]]) || helper([...nextArr, arr[j] - arr[i]]) || helper([...nextArr, arr[i] * arr[j]]) || helper([...nextArr, arr[i] / arr[j]]) || helper([...nextArr, arr[j] / arr[i]]) } } return valid } return helper(arr) } console.log(canOperate(arr, target));
Output
true
- Related Articles
- Generating desired pairs within a range using JavaScript
- Generating desired combinations in JavaScript
- Triplet with desired sum in JavaScript
- Why am I not getting the desired results despite working very hard?
- How to convert JSON results into a date using JavaScript?
- Finding three desired consecutive numbers in JavaScript
- Binary subarrays with desired sum in JavaScript
- Checking power of 2 using bitwise operations in JavaScript
- Finding desired numbers in a sorted array in JavaScript
- Constructing a string of alternating 1s and 0s of desired length using JavaScript
- Check if string ends with desired character in JavaScript
- Finding desired sum of elements in an array in JavaScript
- Interpreting stat() results using Python
- How to plot 2d FEM results using matplotlib?
- Arithmetic operations using OpenCV in Python

Advertisements