简体   繁体   中英

Naming a function in javascript for-loops

I need to create a loop that prints out the numbers 1-75 excluding multiples of 7 and replacing them with a quote. What I need help with is that I need to name the function (ex: forLoop).

this is my code, is it correct?

var forLoop= function() {
    for (var i= 1; i < 76; i++) {
        var skip = 0;
        if (i % 7 == 0) {
            console.log("Not all who wander are lost");
            skip = 1;
        }
        if (!skip) {
            console.log(i);
        }
    }

Looks like you're just missing a closing brace (for the function) and the invocation:

var forLoop= function() {
    for (var i= 1; i < 76; i++) {
        var skip = 0;
        if (i % 7 == 0) {
            console.log("Not all who wander are lost");
            skip = 1;
        }
        if (!skip) {
            console.log(i);
        }
    }
};

forLoop();

@Trott的答案很好,但仅出于大笑,这是一个单行代码,可做同样的事情:

(if i % 7 is 0 then console.log "Not all who wander are lost" else console.log i) for i in [1..76]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM