Index

Assignment 09 - Truncate string

Write a JavaScript function to remove specified number of characters from a string.
Test Data: console.log(truncate_string("Robin Singh”,4)); "Robi"

Enter a string and desired number of character to truncate. Meaning, the number of characters you want to be visible. So, '0' means empty string.



$(document).ready(function() { function truncateString(text, length) { return text.slice(0, length); } $('.truncate').click(function() { var text = $('.data input.string').val(), length = $('.data input.number').val(); $('.result p').html('Truncated string is: "<span>' + truncateString(text, length) + '</span>".'); }); });