
- 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
Validating string with reference to array of words using JavaScript
Problem
We are required to write a JavaScript function that takes in a sequence of valid words and a string. Our function should test if the string is made up by one or more words from the array.
Input
const arr = ['love', 'coding', 'i']; const str = 'ilovecoding';
Output
const output = true;
Because the string can be formed by the words in the array arr.
Example
Following is the code −
const arr = ['love', 'coding', 'i']; const str = 'ilovecoding'; const validString = (arr = [], str) => { let arrStr = arr.join(''); arrStr = arrStr .split('') .sort() .join(''); str = str .split('') .sort() .join(''); const canForm = arrStr.includes(str); return canForm; }; console.log(validString(arr, str));
Output
true
- Related Articles
- Validating a string with numbers present in it in JavaScript
- Validating a password using JavaScript
- Validating brackets in a string in JavaScript
- Validating a boggle word using JavaScript
- If string includes words in array, remove them JavaScript
- Returning lengthy words from a string using JavaScript
- Order an array of words based on another array of words JavaScript
- Replace words of a string - JavaScript
- How to find specific words in an array with JavaScript?
- Summing array of string numbers using JavaScript
- Validating a power JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- Way of validating RadioBoxes with jQuery?
- Convert string with separator to array of objects in JavaScript
- Validating email and password - JavaScript

Advertisements