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.

Respuesta :

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