
- 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
Numbers and operands to words in JavaScript
Problem
We are required to write a JavaScript function that takes in a string of some mathematical operation and return its literal wording.
Example
Following is the code −
const str = '5 - 8'; const convertToWords = (str = '') => { const o = { "+" : "Plus", "-" : "Minus", "*" : "Times", "/" : "Divided By", "**" : "To The Power Of", "=" : "Equals", "!=" : "Does Not Equal", } const n = { 1 : "One", 2 : "Two", 3 : "Three", 4 : "Four", 5 : "Five", 6 : "Six", 7 : "Seven", 8 : "Eight", 9 : "Nine", 10 : "Ten", } let t = str.split(' ') let y = '' let c = 0 for (const [key, value] of Object.entries(o)) { if(key !== t[1]) c++; } if(c === Object.keys(o).length) return "That\'s not an operator!" for (const [key, value] of Object.entries(n)) { if(key === t[0]) y += `${value} ` } for (const [key, value] of Object.entries(o)) { if(key === t[1]) y += `${value}` } for (const [key, value] of Object.entries(n)) { if(key === t[2]) y += ` ${value}` } return y; } console.log(convertToWords(str));
Output
Five Minus Eight
- Related Articles
- How to turn words into whole numbers JavaScript?
- JavaScript function to convert Indian currency numbers to words with paise support
- What are Instruction Codes and Operands in Computer Architecture?
- Evaluation order of operands in C++
- How to convert numbers to words using Python?
- How to Convert Numbers to Words in Indian Rupees in Excel?
- Function to check two strings and return common words in JavaScript
- JavaScript code to extract the words in quotations
- Calculating time taken to type words in JavaScript
- How to replace numbers written in words with numbers in an R data frame column?
- What are Reserved Words in JavaScript?
- Expressive words problem case in JavaScript
- Reversing words in a string in JavaScript
- Finding words in a matrix in JavaScript
- Adding and searching for words in custom Data Structure in JavaScript

Advertisements