Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a novice, can you help me interpret this code?(javascript)
Part I:
JavaScript
function zhuanhuan() {
    if (document.getElementById("txtJSON").value == "") {
        alert('请输入单词!');
    } else {
            $(".display").empty();
            var words = $('#txtJSON').val().split("\n");
            for (i = 0; i < words.length; i++) {
                $(".display").append("<div class='word'><a class='" + words[i] + "' onclick='pronance(this)'>" +
                    words[i] + "</a></div>");


Part II:
JavaScript
$("button#dictation").click(function () {
    var childs = $(".display").find("div > a");
    var pn = document.getElementsByName("proun");
    var p = 1;
    for (n = 0; n < pn.length; n++) {
        if (pn[n].checked) {
            p = pn[n].value;
        }
    }
    var i = 0;
    var t = document.getElementById("time").value;

    function myloop() {
        setTimeout(function () {
            var audio = $("#audio");
            audio.attr("src", "http://dict.youdao.com/dictvoice?audio=" + $(childs[i]).attr("class") +
                "&type=" + p);
            audio.get(0).play();
            i++;
            if (i < childs.length) {
                myloop();
            }
        }, 1000 * t);
    }

    myloop();

});


The second part is the most important! be deeply grateful!

What I have tried:

Part I:
JavaScript
function zhuanhuan() {
    if (document.getElementById("txtJSON").value == "") {
        alert('请输入单词!');
    } else {
            $(".display").empty();
            var words = $('#txtJSON').val().split("\n");
            for (i = 0; i < words.length; i++) {
                $(".display").append("<div class='word'><a class='" + words[i] + "' onclick='pronance(this)'>" +
                    words[i] + "</a></div>");


Part II:
JavaScript
$("button#dictation").click(function () {
    var childs = $(".display").find("div > a");
    var pn = document.getElementsByName("proun");
    var p = 1;
    for (n = 0; n < pn.length; n++) {
        if (pn[n].checked) {
            p = pn[n].value;
        }
    }
    var i = 0;
    var t = document.getElementById("time").value;

    function myloop() {
        setTimeout(function () {
            var audio = $("#audio");
            audio.attr("src", "http://dict.youdao.com/dictvoice?audio=" + $(childs[i]).attr("class") +
                "&type=" + p);
            audio.get(0).play();
            i++;
            if (i < childs.length) {
                myloop();
            }
        }, 1000 * t);
    }

    myloop();

});
Posted
Updated 24-Jun-20 22:30pm

Quote:
I am a novice, can you help me interpret this code?

One of the best way to understand a piece of code is to watch it perform, the tool of choice is debugger, secondary effect of abusing of debugger is that it improve your learning curve.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

a href="https://en.wikipedia.org/wiki/Debugger">Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900