Question:
How does it work and how do the parameters of a function work?
var form = document.getElementById('form');
function mifuncion(t,r) {
console.log('Sip');
}
<form id="form">
<input type="text" id="dato1" required>
<input type="text" id="dato2" required>
<button onclick="mifuncion(event)">Enviar</button>
</form>
Answer:
Well, first you have to know that a "function" is a piece of code that does a specific function or task.
A basic example is a mathematical calculation. Look at this function:
function sumar( a, b){
console.log(a + b)
}
This function for it to work, it needs 2 parameters which are a
and b
(2 data or variables) for it to work
Let's call a function passing it the parameters:
sumar( 1, 2)
This what it will do is add 1 + 2 and it will show you the result in the console
Let's see another example: I'm going to make a function that repeats a code for me according to the number of times I pass the function through parameters
function repetir( repeticiones ){
for ( var i = 1; i <= repeticiones; i++){
console.log('repeticion numero' + i)
}
}
we call the function
repetir(4) // repetira el codigo 4 veces
repetir(1000) // repetira el codigo mil veces