## 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 `for`

loop, 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

## Top comments (0)