
- 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
Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - 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
- Related Articles
- Concatenation of two strings in PHP program
- C++ program to get length of strings, perform concatenation and swap characters
- C++ program to find uncommon characters in two given strings
- Java Program to check if the String contains any character in the given set of characters
- Python program to replace all Characters of a List except the given character
- Program to find total number of strings, that contains one unique characters in Python
- Find uncommon characters of the two strings in C++ Program
- Smart concatenation of strings in JavaScript
- Count of strings that become equal to one of the two strings after one removal in C++
- Concatenation of two strings in PHP\n
- Delete every one of the two strings that start with the same letter in JavaScript
- Check if concatenation of two strings is balanced or not in Python
- Program to partition two strings such that each partition forms anagram in Python
- Python Program – Strings with all given List characters
- JavaScript Program to Find a triplet such that sum of two equals to third element

Advertisements