
- 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
Complete Equation by Filling Missing Operator in JavaScript
We are required to write a JavaScript function that takes in a bunch of numbers and returns the correct sequence of operations to satisfy the equation. The operator that can be used are (+, −, *, /, ^, %).
For example −
Input : 5 3 8 Output : 5+3=8 Input : 9 27 3 Output : 9=27/3 Input : 5 2 25 , 1 5 2 Output : 5^2=25 , 1=5%2
For each input, there is at least one possible sequence, we are required to return at least one correct sequence.
The algorithm we are going to use to solve this problem is −
Firstly, we choose the bigger number on one of the sides like in 1 4 7 it would be 7
Then we put an equal to facing the middle. Like 1 4 7 it would be 1 4=7
Lastly, we solve the equation
If that doesn't work, we try the other number
Example
The code for this will be −
const arr = ["5 3 8", "9 27 3", "5 2 25", "1 5 2", "3 3 3 30"]; const findCombination = (arr = []) => { const answers = []; for(let i = 0; i < arr.length; i++){ const el = arr[i]; // using brute force to try solutions for(let n = 0; n < 1000; n++){ const s = el.replace(/ /g, () => "+− */^%="[Math.floor(Math.random() * 7)]); if(eval(s.replace(/=/g, "===").replace(/\^/g, "**")) === true && answers.indexOf(s) === −1){ answers.push(s); }; }; } return answers; }; console.log(findCombination(arr));
Output
And the output in the console will be −
[ '5+3=8', '9=27/3', '5^2=25', '1=5%2', '3=3%3^30', '3^3+3=30', '3+3^3=30' ]
- Related Articles
- Python Pandas - Filling missing column values with mode
- Python Pandas - Filling missing column values with median
- Comparing and filling arrays in JavaScript
- Adding numbers represented by string without complete conversion in JavaScript
- Complete the following equation :Zn + 2HCl ——> __+ __
- Instanceof operator in JavaScript
- JavaScript Auto-filling one field same as other
- Missing letter from strings in JavaScript
- Complete Graph Class in Javascript
- Explain exponentiation operator in JavaScript?
- Explain Grouping operator in JavaScript.
- The new operator in JavaScript
- Optional chaining operator in JavaScript.
- Finding missing letter in a string - JavaScript
- JavaScript Spread Operator

Advertisements