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.

Pictorial 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"));

Sample 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"));

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.



Follow us on Facebook and Twitter for latest update.