Welcome to Westonci.ca, where you can find answers to all your questions from a community of experienced professionals. Join our platform to connect with experts ready to provide accurate answers to your questions in various fields. 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

Thanks for using our service. We aim to provide the most accurate answers for all your queries. Visit us again for more insights. We hope this was helpful. Please come back whenever you need more information or answers to your queries. Thank you for visiting Westonci.ca. Stay informed by coming back for more detailed answers.