w3resource

JavaScript: Test whether a string end with "Script"

JavaScript Basic: Exercise-65 with Solution

Write a JavaScript program to test whether a string ends with "Script". The string length must be greater than or equal to 6.

The JavaScript program checks if a given string ends with "Script" and ensures the string's length is at least 6 characters. If both conditions are met, it returns true; otherwise, it returns false.

Visual Presentation:

JavaScript: Test whether a string end with 'Script'

Sample Solution:

JavaScript Code:

// Define a function named end_script with parameter str
function end_script(str) {
  // Check if the last 6 characters of str are equal to 'Script'
  if (str.substring(str.length - 6, str.length) == 'Script') {
    // Return true if the condition is met
    return true;
  } else {
    // Return false if the condition is not met
    return false;
  }
}

// Call the function with sample arguments and log the results to the console
console.log(end_script("JavaScript"));
console.log(end_script("Java Script"));
console.log(end_script("Java Scripts"));

Output:

true
true
false

Live Demo:

See the Pen JavaScript - Test if a string end with Script - basic-ex-65 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Test whether a string end with

ES6 Version:

// Define a function named end_script with parameter str
const end_script = (str) => {
    // Use endsWith method to check if str ends with 'Script' and return the result
    return str.endsWith('Script');
};

// Call the function with sample arguments and log the results to the console
console.log(end_script("JavaScript"));
console.log(end_script("Java Script"));
console.log(end_script("Java Scripts"));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to concatenate two strings and return the result.
Next: JavaScript program to display the city name if the string begins with "Los" or "New" otherwise return blank.

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://w3resource.com/javascript-exercises/javascript-basic-exercise-65.php