Skip to content

Instantly share code, notes, and snippets.

@geraldotech
Last active December 17, 2022 14:56
Show Gist options
  • Save geraldotech/017b5431d853b37af18e0c7fbab97246 to your computer and use it in GitHub Desktop.
Save geraldotech/017b5431d853b37af18e0c7fbab97246 to your computer and use it in GitHub Desktop.
exercicios special
//first class function
const ano = [1992,1995,2000,2006,2008];
function idade(age){
let d = new Date();
return d.getFullYear() - age;
}
function Calc(ano,fun){ // declarando dois parâmetros, os anos e a função que calcula a idade baseado nos anos
let gets = []; // array vazio vai armazerar os resultados
for(i = 0; i < ano.length; i++){
gets.push(fun(ano[i])); // array usa a fun que empurra os dados da funcao idade no index max depende da quant de anos
}
return gets; //retorno da function
}
}
let todos = Calc(ano,idade); // funcao Calc recebe seus dois argumentos
console.log(todos); //[30, 27, 22, 16, 14];
const arr = ["Alpha","Bravo","Charlie","Deltra","Echo","Foxtrot","Golf","Hotel"];
//1 podemos definir o element pai direto:
document.getElementById("root").innerHTML = `
<ul>
${arr.map((html)=> {
return `<li>${html}</li>`
}).join('')}
</ul>
`;
//1.2 ou no próprio HTML
document.getElementById("list").innerHTML = arr.map((val) => `<li>${val}</li>`);
//no HTML ficaria assim:
<ul id="list"></ul>
//2
function meuhtml(arr,fun){
const get = [];
for(value of arr){
get.push(fun(value));
}
return get.join('');
}
function meuli(myli){
return `<li>${myli}</li>`;
}
const show = meuhtml(arr,meuli);
document.getElementById("root2").innerHTML = `<ol>${show}</ol>`;
//3
const show2 = meuhtml(arr,(pegali)=>{
return `<li>${pegali}</li>`;
})
document.getElementById("root3").innerHTML = `<ol>${show2}</ol>`;
//👉UPDATE - usar um for of é bem desnecessário visto que a forma concise abaixo funciona.
function meuhtml(arr){
return arr.map(val => `<li>${val}</li>`).join("");
}
document.getElementById("list2").innerHTML = `<ol>${meuhtml(itens)}</ol>`;
//e como já sabemos basta apenas inline version
${arr.map((el)=> `<li>${el}</li>`).join('')}
//4
arr.forEach((el, index)=> console.log(index,el));
//5
//console.log(Object.keys(arr));
for(value of arr.keys()){
console.log(value);
}
//6 html Click-box-inside-outsite.html
//7
function register(name,age,city){
this.name = name;
this.age = age;
this.city = city;
}
const char = new register("OROCHI", 15,"Mcz");
console.log(char);
//8
var gato = {
alimento: "ração",
comer: function(){
console.log("está commendo ", this.alimento);
}
}
gato.comer();
var leao = {
alimento: "carne",
}
leao.comer = gato.comer;
leao.comer();
//9
https://codepen.io/geraldopcf/pen/dymJMVL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment