1

I have some problems here.

a)I would like to set the 3-8 characters,but it only show 3 characters.

b)I would like to have three output with different display time.Do I only nid to create different Id?

Anyone can help? What's wrong with this code? Thanks.

Javascript

 function randomString(Length) { if(Length < 3) Length = 3; if(Length > 8) Length = 8; var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for( var i=0; i < Length; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } function ChangingRandomString(Length) { setInterval(function(){document.getElementById("random").style.fontSize = Math.floor((Math.random() * 20) + 10)+"px", document.getElementById("random").innerHTML = randomString(Length); },2000); } 
2
  • Where are you calling ChangingRandomString, what length did you pass? How does your HTML look like? Commented Aug 22, 2017 at 0:51
  • Yes, create some different ids, and make the element another parameter of ChangingRandomString so that you can call it three times. Commented Aug 22, 2017 at 0:52

2 Answers 2

1

function randomString(length) { if (length < 3) length = 3; if (length > 8) length = 8; var text = ''; var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; for (var i = 0; i < length; i++) { text += possible[Math.floor(Math.random() * possible.length)]; } return text; } function ChangingRandomString(length) { setInterval(() => { let el = document.getElementById('random'); el.style.fontSize = Math.floor(Math.random() * 20 + 10) + 'px'; el.innerHTML = randomString(length); }, 2000); } ChangingRandomString(length);
<div id="random"></div>

Sign up to request clarification or add additional context in comments.

Comments

0
function randomString(length){ if(length<5) length =5; if(length>8) length =8; var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(let i=0;i<=length;i++){ text +=possible[Math.floor(Math.random()*possible.length)] } return text } function string(length){ setInterval(() => { console.log(randomString(length)); },2000); } string(5); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.