Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Умная машина (https://javascript.ru/forum/misc/66590-umnaya-mashina.html)

Nlk 25.12.2016 20:32

Умная машина
 
Всем Здравствуйте!
Изучаю книжку по JavaScript, в которой, застопорился на одном упражнении. Просьба описать основы работы данного кода.
Заранее спасибо


<script>
function clunk(times) {
var num = times;
while (num > 0) {
display("clunk");
num = num - 1;
}
}

function thingamajig(size) {
var facky = 1;
clunkCounter = 0;
if (size == 0) {
display("clank");
} else if (size == 1) {
display("thunk");
} else {
while (size > 1) {
facky = facky * size;
size = size - 1;
}
clunk(facky);
}
}

function display(output) {
console.log(output);
clunkCounter = clunkCounter + 1;
}
var clunkCounter = 0;
thingamajig(0);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(1);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(2);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(3);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(4);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(5);
console.log(clunkCounter);

</script>

Paguo-86PK 25.12.2016 22:10

<script>
var clunkCounter = 0;

function clunk(times) {
    var num = times;
    while(num > 0) {
        display("clunk");
        num --;
    }
}

function thingamajig(size) {
    var facky = 1;
    clunkCounter = 0;
    if(size == 0) {
        display("clank");
    } else
    if(size == 1) {
        display("thunk");
    } else {
        facky = size * (size + 1) / 2;
    }
    clunk(facky);
}

function display(output) {
    console.log(output);
    clunkCounter = clunkCounter + 1;
}

thingamajig(0);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(1);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(2);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(3);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(4);
console.log(clunkCounter);

clunkCounter = 0;
thingamajig(5);
console.log(clunkCounter);
</script>


Часовой пояс GMT +3, время: 23:20.