We help IT Professionals succeed at work.

Html Newline

Whing Dela Cruz
on
112 Views
Last Modified: 2017-03-12
Hi experts, I want to make 3 lines of the following sentence below. How? I wish to make 3 lines. Thanks!

         var mm = "the quick brown fox jumped over the lazy dog"
         alert(mm);
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Leonidas, I've tried the code to achieve the result of 3 lines but it doesn't work.  I wish the result like this, which is in three lines

The
quick
brown

alert("The"+'\r'+"quick"+'\r'+"brown");
Software Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
n is working on my project. Thank you Ryan Chong!

Author

Commented:
Thanks a lot both of you Guys!
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
First i count the lenght of the string  and the divide this  with 3 to get the number of charachters of eaxh chunk.The follow the code that takes two arg.String anf the lenght that you found above

var mm = "the quick brown fox jumped over the lazy dog";
console.log(mm.length/3);

function chunkString(str, len) {
  var size = Math.ceil(str.length/len),
      retStr  = new Array(size),
      offset
  ;

  for (var i=0; i<size; i++) {
    offset = i * len;
    retStr[i] = str.substring(offset, offset + len);
  }
  console.log(retStr);
  return alert(retStr[0]+"\r"+retStr[1]+"\r"+retStr[2]);
  
}
chunkString(mm, 16);

Open in new window

At the end you can mofify the len arg so that you have all the words without slice

Author

Commented:
Thank you so much Leonidas for giving me such brilliant techniques. More power and God bless!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.