Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rubik's Cube scrambler giving all of the same move

I am new to JavaScript loops, and I am making a Rubik’s Cube scrambler. It generates random moves and displays them fine, but all of them are the same move. I think there is a way of fixing it by having 20 scramble variables but it would be inefficient and too long. Is there any compact way of fixing this? This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speedcubing Timer</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <style>
        * {
            padding:20px;
        }
    </style>
</head>
<body>
<h1>CubeTimer v1.0</h1>
    <div class="row">
        <div class="col-xs-5">
            <h1>Scramble</h1>
            <h3 id="scramble"></h1>
        </div>
    </div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script>
let scramble = Math.floor(Math.random() * 12);
function scramble_translator() {
    if (scramble <= 1) {
        scramble = 'R '
    } else if (scramble > 1 && scramble <= 2) {
        scramble = 'R\' '
    } else if (scramble > 2 && scramble <= 3) {
        scramble = 'U '
    } else if (scramble > 3 && scramble <= 4) {
        scramble = 'U\' '
    } else if (scramble > 4 && scramble <= 5) {
        scramble = 'L '
    } else if (scramble > 5 && scramble <= 6) {
        scramble = 'L\' '
    } else if (scramble > 6 && scramble <= 7) {
        scramble = 'F '
    } else if (scramble > 7 && scramble <= 8) {
        scramble = 'F\' '
    } else if (scramble > 8 && scramble <= 9) {
        scramble = 'D '
    } else if (scramble > 9 && scramble <= 10) {
        scramble = 'D\' '
    } else if (scramble > 10 && scramble <= 11) {
        scramble = 'B '
    } else if (scramble > 11 && scramble <= 12) {
        scramble = 'B\' '
    }

    document.getElementById('scramble').innerHTML += scramble;
}
for (let i = 0; i < 20; i++) {
    scramble_translator();
}
</script>
</body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Move random generation code into scramble_translator.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speedcubing Timer</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <style>
        * {
            padding:20px;
        }
    </style>
</head>
<body>
<h1>CubeTimer v1.0</h1>
    <div class="row">
        <div class="col-xs-5">
            <h1>Scramble</h1>
            <h3 id="scramble"></h1>
        </div>
    </div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script>
function scramble_translator() {
    let scramble = Math.floor(Math.random() * 12);
    if (scramble <= 1) {
        scramble = 'R '
    } else if (scramble > 1 && scramble <= 2) {
        scramble = 'R\' '
    } else if (scramble > 2 && scramble <= 3) {
        scramble = 'U '
    } else if (scramble > 3 && scramble <= 4) {
        scramble = 'U\' '
    } else if (scramble > 4 && scramble <= 5) {
        scramble = 'L '
    } else if (scramble > 5 && scramble <= 6) {
        scramble = 'L\' '
    } else if (scramble > 6 && scramble <= 7) {
        scramble = 'F '
    } else if (scramble > 7 && scramble <= 8) {
        scramble = 'F\' '
    } else if (scramble > 8 && scramble <= 9) {
        scramble = 'D '
    } else if (scramble > 9 && scramble <= 10) {
        scramble = 'D\' '
    } else if (scramble > 10 && scramble <= 11) {
        scramble = 'B '
    } else if (scramble > 11 && scramble <= 12) {
        scramble = 'B\' '
    }

    document.getElementById('scramble').innerHTML += scramble;
}
for (let i = 0; i < 20; i++) {
    scramble_translator();
}
</script>
</body>
</html>

You can make your code shorter like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speedcubing Timer</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <style>
        * {
            padding:20px;
        }
    </style>
</head>
<body>
<h1>CubeTimer v1.0</h1>
    <div class="row">
        <div class="col-xs-5">
            <h1>Scramble</h1>
            <h3 id="scramble"></h1>
        </div>
    </div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script>
function scramble_translator() {
    let scramble = Math.floor(Math.random() * 12);
    const table = ['R','R\'','U','U\'','L','L\'','F','F\'','D','D\'','B','B\'',];

    document.getElementById('scramble').innerHTML += table[scramble] + ' ';
}
for (let i = 0; i < 20; i++) {
    scramble_translator();
}
</script>
</body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading