- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting rings in letters using JavaScript
Problem
We are required to write a JavaScript function that takes in a string of English alphabets.Our function should count the number of rings present in the string.
O', 'b', 'p', 'e', 'A', etc. all have one rings whereas 'B' has 2
Example
Following is the code −
const str = 'some random text string'; function countRings(str){ const rings = ['A', 'D', 'O', 'P', 'Q', 'R', 'a', 'b', 'd', 'e', 'g', 'o', 'p', 'q']; const twoRings = ['B']; let score = 0; str.split('').map(x => rings.includes(x) ? score++ : twoRings.includes(x) ? score = score + 2 : x ); return score; } console.log(countRings(str));
Output
7
Advertisements