Skip line in a function with implementation

Asked

Viewed 95 times

0

I created a function pularLinha() so you don’t have to keep repeating <br> constantly, however to make the line jump more than once inside the function mostra(), I call pularLinha() twice.

There I think and if it were necessary to jump 5 in 5 lines, I would call the same function 5 times within the function mostra()? That would look awful!

I tried to put an implementation to improve the code and stay functional, but I could not.

    let pulaLinha = function (){
        document.write("<br>");
    };

    let mostra = function(frase){
        document.write(frase);
        pulaLinha(); pulaLinha();

    }

    let ano = 2012; 
    mostra("Eu nasci em : " + (ano - 25)); 
    mostra("Adriano nasceu em : " + (ano - 26));
    mostra("Paulo nasceu em : " + (ano - 32)); 
  • If you need to repeat the same thing over and over again, use a for. Another detail, to declare functions, simply do function mostra(frase) { etc}. In that case there’s no point in doing mostra = function(etc..): https://answall.com/q/13364/112052 | https://stackoverflow.com/a/33040926

  • Another thing I think is wrong there is that you keep giving one Document.write on top of another, Document.write overwrites all html.

1 answer

4


First, let’s get this abuse of lambda where a normal function works best. If someone taught like this, run away.

I don’t think it’s horrible for a code to show that it does exactly what it’s meant to do. If he must skip two lines then call the code that does what he wants twice. If you want to do this 5 times, although I think it’s too much to use 5 times. But if really need and are often (do not make for 2) then just make a loop.

function pulaLinha() {
    document.write("<br>");
};

function mostra(frase) {
    document.write(frase);
    for (let i = 0; i < 5; i++) pulaLinha();
}

let ano = 2012; 
mostra("Eu nasci em : " + (ano - 25)); 
mostra("Adriano nasceu em : " + (ano - 26));
mostra("Paulo nasceu em : " + (ano - 32)); 

I put in the Github for future reference.

I find it a huge exaggeration, but if you want you can parameterize the function that jumps line with a repetition number:

function pulaLinha(repeticao = 1) {
    for (let i = 0; i < repeticao; i++) document.write("<br>");
};

function mostra(frase) {
    document.write(frase);
    pulaLinha(2);
}

let ano = 2012; 
mostra("Eu nasci em : " + (ano - 25)); 
mostra("Adriano nasceu em : " + (ano - 26));
mostra("Paulo nasceu em : " + (ano - 32)); 

I put in the Github for future reference.

Just remembering that the let and the argument default works in modern interpreters, so it will be used in Node, Deno or similar things or can guarantee that it will run in modern browser or use a transpilator for older Ecmascipt, so you can use, if not var.

  • 1

    +1 for "fix this lambda abuse", nothing like a good vanilla code, easy to understand :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.