Welcome to Westonci.ca, the Q&A platform where your questions are met with detailed answers from experienced experts. Discover precise answers to your questions from a wide range of experts on our user-friendly Q&A platform. Join our platform to connect with experts ready to provide precise answers to your questions in different areas.

write a function fullwords(s) that returns a string no longer than 20 characters. if the string s is longer than 20 characters, just truncate at the space closest to the 20th position (so no word is truncated) and return it. if the string s is shorter 20 characters, just return it unchanged.

Sagot :

Assuming you mean a function that truncates a string to 20 characters:

function fullwords(s){

if (s.length > 20) {

//find the 20th character

var char20 = s.charAt(19);

//if it's a space, return the string up to that point

if (char20 == " ") {

return s.substring(0, 20);

}

//if it's not a space, find the closest space

else {

var spaceBefore = s.lastIndexOf(" ", 18);

return s.substring(0, spaceBefore);

}

}

//if the string is shorter than 20 characters, return it unchanged

else {

return s;

}

}

To use fullwords(), simply pass in a string as the only argument. The function will then return a string that is no longer than 20 characters. If the string you passed in was shorter than 20 characters, it will be returned unchanged.

Learn more on string here:

https://brainly.com/question/28290531

#SPJ4

Thank you for choosing our service. We're dedicated to providing the best answers for all your questions. Visit us again. Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. Your questions are important to us at Westonci.ca. Visit again for expert answers and reliable information.