
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Append Two Strings Omitting Double Characters in JavaScript
We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string.
If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let’s say the following are our strings in JavaScript −
const str1 = 'Food'; const str2 = 'dog';
Let’s write the code for this function −
const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings = (str1, str2) => { const { length: l1 } = str1; const { length: l2 } = str2; if(str1[l1 - 1] !== str2[0]){ return str1 + str2; }; const newStr = str2.substr(1, l2 - 1); return str1 + newStr; }; console.log(concatenateStrings(str1, str2));
Output
Following is the output in the console −
Foodog
Advertisements