DEV Community

Zack
Zack

Posted on

2 1

Calculating the Possible Rubik's Cube Combos

Hello everyone!

Today I will show you how you can calculate how many Rubik's cube combinations there are.

(This works on both web and Node!)

The correct output is at the end of the post :)

We'll start by making a function in our Javascript file, let's call it calc.

function calc() {
}

Make sure to give it two arguments, x and y, just like so:

function calc(x, y) {
}

Now, inside of this function, define a variable called j.

function calc(x, y) {
  var j = 0;
}

We'll use this variable as the output from the function, so calc(1, 1) would output y, which would be equal to 1.

Make your for loop. It will help us calculate the value.

function calc(x, y) {
  var j = 0;
  for(var i = 0; i < y; i++) {
  }
}

In our forloop, it does 3 things (in order):

  • defines the initial i variable.
  • makes sure i is less than y.
  • adds one to i.

That's great, now add an if-else statement that checks if j is equal to 0.

function calc(x, y) {
  var j = 0;
  for(var i = 0; i < y; i++) {
    if(j == 0) {
    } else {
    }
  }
}

Why are we doing this?

The formula is that j is set to j * x for y times. It will always be 0 if we don't set that initial value.

Let's finish up our function!

function calc(x, y) {
  var j = 0;
  for(var i = 0; i < y; i++) {
    if(j == 0) {
      j = x;
    } else {
      j = j * x;
    }
  }
}

Absolutely DON'T forget to return the value of j after that for loop!

function calc(x, y) {
  var j = 0;
  for(var i = 0; i < y; i++) {
    if(j == 0) {
      j = x;
    } else {
      j = j * x;
    }
  }
  return j;
}

Okay, what now!?

So how do I sum up the number of possible combinations of Rubik's cube with this function?

There are 6 sides to a Rubik's cube; each with a different color. Each side has 9 tiles, with 6 possible colors.

We'd work it out like this:

calc(calc(6, 9), 6);

Just print it out to the console with console.log, your code should look like this:

function calc(x, y) {
  var j = 0;
  for(var i = 0; i < y; i++) {
    if(j == 0) {
      j = x;
    } else {
      j = j * x;
    }
  }
  return j;
}


console.log(calc(calc(6, 9), 6));

It will output this:

> 1.0475325355943345e+42

That is our answer, 1.0475325355943345e+42.

Thanks for reading everyone! <3

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️