Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ROW_LIMIT = 15;
- const text = "this is an Example Hello how are you today gentlemen life is life nanana na na na"
- class Row {
- constructor() {
- this.Words = []
- this.NumberOfWords = 0
- this.NumberOfWhitespaces = 0
- this.Length = 0
- }
- wrap(text) {
- if( ((this.Length + this.NumberOfWhitespaces) + text.length) >= ROW_LIMIT) {
- return true
- }
- }
- addWhitespace() {
- if(this.NumberOfWords > 1)
- this.NumberOfWhitespaces++
- }
- addWord(text) {
- this.Words.push(text)
- this.Length += text.length
- this.NumberOfWords++
- this.addWhitespace()
- }
- }
- class Grid {
- constructor(row_length, text) {
- this.rows = []
- this.row_max_length = row_length
- this.Text = text
- this.words = text.split(' ')
- this.RowBuilder()
- }
- RowBuilder() {
- var row = new Row()
- for(let word of this.words) {
- if(row.wrap(word)) {
- this.rows.push(row)
- row = new Row()
- }
- row.addWord(word)
- }
- this.rows.push(row)
- }
- }
- var grid = new Grid(ROW_LIMIT, text)
- console.log(grid.rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement