Created
July 14, 2019 17:52
-
-
Save thiagosouza/5d8fd08f176d366c076defbf45489f6b to your computer and use it in GitHub Desktop.
Javascript Basics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nome = "Thiago"; //global | |
let nomeLet = "Thiago"; //escopo/block | |
const nomeConst = "Thiago";//nao pode ser alterado | |
console.log(typeof ``); | |
var endereco = ""; | |
var endereco = "Thiago de Souza"; | |
var endereco = 'Thiago disse:"Eu sou eu!"'; | |
var endereco = "Thiago disse:\"Eu sou eu!\""; | |
var jardineiro = "Jesus" | |
var endereco = `Thiago disse: "eu sou eu" e completou 'vc é vc' e o jardineiro é ${jardineiro}`; | |
console.log(endereco); | |
// definição da funcao | |
function doSomething(name = ["Ze"]) { //function doSomething(name){ | |
console.info("estou retornando um nome"); | |
return name; //funcoes zicas retornam coisas | |
} | |
console.log(doSomething); //nao chama a funcao | |
console.log(doSomething(nome)); //chama a funcao com () | |
console.log(doSomething({ nome: 'Thiago', nome: 'Lucas' })); //chama a funcao com () | |
// EStruturas de dados nativas do JS | |
// var, let, const | |
// [0, "", [{}, []], {'key':{}}] array 0 1 | |
// {"key":"value"} | |
// regex - regular expression | |
var cpf = "111-222-333-44"; https://regex101.com/r/KsvUO4/2 | |
console.log("cpf", cpf.replace(/[^\d]/g, "")); //regex replace | |
console.log("13/12/1987".replace(/(\d{2})\/(\d{2})\/(\d{4})/g, `$3-$2-$1`)); | |
// estrutura de dados / objeto / JSON | |
var person = { | |
"nome": "Thiago", | |
"email": "[email protected]", | |
"cpf": "111.222333-44", | |
"address": [{ | |
"street": "Rua Bela Cintra", | |
"number": 555, | |
"complement": "apt 555", | |
"city": "São Paulo" | |
}, { | |
"street": "Rua Bela Cintra", | |
"number": "555", | |
"complement": "apt 555", | |
"city": "São Paulo" | |
}], | |
"phones": ["+5511999998888", "+5511999998887"], | |
"phonesFrom": { | |
"home": "+5511999998888", | |
"work": "+5511999998887" | |
} | |
} | |
// console.log(person.address[0], person.phones[0], person.phonesFrom.home) | |
// function getName(callback) { | |
// //binance get names | |
// var names = ["nome1", "nome2"]; | |
// for(i=0; i<2;i++){ | |
// callback(array[i]); //runs 2 times | |
// } | |
// } | |
// function print(name) { | |
// console.log(name); | |
// } | |
// getName((name)=> { console.log(item, i, array)}) | |
var arrayNomes = ["Thiago1", "Ze1"]; | |
arrayNomes.forEach(function print(nome) { | |
console.log(nome) | |
}); | |
arrayNomes.forEach(function (nome) { | |
console.log(nome) | |
}); | |
arrayNomes.forEach((nome) => { //mais usado!!!! | |
console.log(nome) | |
}); | |
arrayNomes.forEach(nome => { | |
console.log(nome) | |
}); | |
arrayNomes.forEach(nome => console.log(nome)); | |
// function name(param1, param2){} | |
// ou | |
// (param1, param2)=>{} | |
// printNames(function lucas(){ console.log("funcao do lucas") }) | |
// JSON | |
// let ordersOriginal = [{ | |
// key: value, | |
// key2: value2 | |
// }] | |
//array documentation | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array | |
var orders = []; | |
var order1 = { amount: "amountA"}; | |
orders.push({key:"value", order1}); // = orders.push({key:"value", amount : { amount: "amountA"}}); | |
orders.push({key:"value", ...order1}); // = orders.push({key:"value", amount : "amountA"}); | |
orders.push({key:"value2"}); | |
orders.push({key:"value2"}); | |
console.log(orders); | |
//defining trade object for shorts positions | |
var trade1 = { | |
type : "short", | |
sell : [{orderId:"xxx"}, {orderId:"xxx2"}], | |
sellAmount : 1, | |
buy : [{orderId:"xxx3"}], | |
buyAmount : 1.1 | |
// profitPercentage : 0.46 | |
}; | |
function calculateProfitPercentage(trade){ | |
let profitPercentage; | |
if(trade.type == "short"){ | |
// calcular quanto vendeu e quanto comprou | |
// eu tinha 1btc, | |
// agora eu tenho 1.1 | |
profitPercentage = 10; | |
} | |
return { ...trade, "profitPercentage": profitPercentage.toFixed(2) } | |
} | |
console.log(trade1) | |
console.log(calculateProfitPercentage(trade1)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment