w3resource

JavaScript: Create a string using the middle three characters of a given string of odd length

JavaScript Basic: Exercise-63 with Solution

Write a JavaScript program to create a string using the middle three characters of a given string of odd length. The string length must be greater than or equal to three.

This JavaScript program extracts and creates a new string consisting of the middle three characters from an input string of odd length, ensuring the input string length is at least three.

Visual Presentation:

JavaScript: Create a string using the middle three characters of a given string of odd length

Sample Solution:

JavaScript Code:

// Define a function named middle_three with parameter str
function middle_three(str) {
    // Check if the length of str is odd
    if (str.length % 2 !== 0) {
        // Calculate the middle index for odd-length strings
        mid = (str.length + 1) / 2;
        // Use slice to get the middle three characters and return
        return str.slice(mid - 2, mid + 1);
    }
    // Return str if its length is not odd
    return str;
}

// Call the function with sample arguments and log the results to the console
console.log(middle_three('abcdefg'));
console.log(middle_three('HTML5'));
console.log(middle_three('Python'));
console.log(middle_three('PHP'));
console.log(middle_three('Exercises')); 

Output:

cde
TML
Python
PHP
rci

Live Demo:

See the Pen JavaScript - Create a string using the middle three characters of a given string of odd length - basic-ex-63 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Create a string using the middle three characters of a given string of odd length

ES6 Version:

// Define a function named middle_three with parameter str
const middle_three = (str) => {
    // Check if the length of str is odd
    if (str.length % 2 !== 0) {
        // Calculate the middle index
        const mid = (str.length + 1) / 2;
        // Use slice to get the middle three characters and return the result
        return str.slice(mid - 2, mid + 1);
    }
    // Return str if its length is not odd
    return str;
};

// Call the function with sample arguments and log the results to the console
console.log(middle_three('abcdefg'));
console.log(middle_three('HTML5'));
console.log(middle_three('Python'));
console.log(middle_three('PHP'));
console.log(middle_three('Exercises'));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to move last three character to the start of a given string.
Next: JavaScript program to concatenate two strings and return the result.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-63.php