0
function generate() {
  const notations = [" U", " D", " R", " L", " F", " B"];

  const switches= ["", "\'", "2"]; 

  let array = [];

  let last = '';

  let random = 0;

  for (let i = 0; i < 20; i++) {
      
      do {
         random = Math.floor(Math.random() * notations.length);
      } while (last == notations[random]) 


  last = notations[random];

  let Item = notations[random] + switches[parseInt(Math.random()*switches.length)];

  array.push(Item);
  }

  let scrambles = "";

  for(i=0; i< 20; i++) {
     scrambles += array[i];
  }

document.getElementById("div").innerHTML = scrambles;
}

so i have function that generates random string so output will be something like R U' B2 R2 L F L2 B U2 B U F' L' B2 R' L D2 U' L2 R

and i want to generate the random letters twice so output will be something like

R U' B2 R2 L F L2 B U2 B U F' L' B2 R' L D2 U' L2 R
B R' U' F' D L U2 F' R' B D U F R' L' F U2 D L R

i found a solution which is by duplicating the codes like this

function generate() {
  const notations = [" U", " D", " R", " L", " F", " B"];

  const switches= ["", "\'", "2"]; 

  let array = [];

  let last = '';

  let random = 0;

  for (let i = 0; i < 20; i++) {
      
      do {
         random = Math.floor(Math.random() * notations.length);
      } while (last == notations[random]) 


  last = notations[random];

  let Item = notations[random] + switches[parseInt(Math.random()*switches.length)];

  array.push(Item);
  }

  let scrambles = "";

  for(i=0; i< 20; i++) {
     scrambles += array[i];
  }

 const notations2 = new Array(" U", " D", " R", " L", " F", " B");

  const switches2= ["", "\'", "2"]; 

  let array2 = [];

  const last2 = '';

  const random2 = 0;

  for (let i = 0; i < 20; i++) {
      
      do {
         random2 = Math.floor(Math.random() * notations2.length);
      } while (last == notations2[random2]) 


  last2 = notations2[random2];

  let Item2 = notations2[random2] + switches2[parseInt(Math.random()*switches2.length)];

  array.push(Item2);
  }

  let scrambles2 = "";

  for(i=0; i< 20; i++) {
     scrambles2 += array[i];
  }
document.getElementById("div").innerHTML = scrambles + "<br>" + scrambles2;
}

but its not efficient, is there a faster way and more efficient way to do this?

7
  • You have "const random = 0;" and later on you apply new value to random. You can't, it's const. (Same with "last") Commented Apr 21, 2021 at 11:49
  • @Dalibor sorry xD i changed it already :D Commented Apr 21, 2021 at 11:53
  • why not simply change the for loop to have the condition be i < 40 instead of i < 20 Commented Apr 21, 2021 at 11:54
  • 1
    @TUTAMKHAMON ah its because i want to add a line break, i have edited the post. um is it possible to do it with i < 40? Commented Apr 21, 2021 at 11:58
  • 1
    Have the function return the generated string. Then you can call it as many times as you want and append the result wherever you want. Commented Apr 21, 2021 at 12:01

2 Answers 2

1

Just have the generate function return the scrambles and call it twice:

function generate(n) {
  const notations = [" U", " D", " R", " L", " F", " B"];
  const switches = ["", "\'", "2"];

  let last = null;
  let scrambles = "";

  for (let i = 0; i < n; ++i) {
    //subtract 1 when you can't select the same as the last
    let available_notations = notations.length - (last === null ? 0 : 1);

    //one random for all combinations
    let random = Math.floor(Math.random() * available_notations * switches.length);

    let nt = Math.floor(random / switches.length); //notation value
    let sw = Math.floor(random % switches.length); //switch value

    if (last !== null && last <= nt) nt += 1; //add 1 when value bigger than last
    last = nt;

    scrambles += notations[nt] + switches[sw];
  }
  return scrambles;
}

document.getElementById("div").innerHTML = generate(20) + '<br/>' + generate(20);
<div id="div"></div>

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

2 Comments

hi thanks for answering, im wondering if there is a way to only have one div element in the html and append the <br> and the paragraph in JS
Yes, edited the answer to reflect that. Also made the generation of random notations more efficient.
1

You have already wrapped your code in a function, so just call it twice (or as many times as you want).

As others have already suggested, simply return your scrambles string from generate() and then you can do something like:

function generateNTimes(n) {
    const scrambles = [];
    for (let i = 0; i < n; i++) {
        scrambles.push(generate());
    }
    document.getElementById("div").innerHTML = scrambles.join('<br>');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.