Created
February 21, 2022 17:43
-
-
Save caioertai/7b9e5eb004b0a844fcc6e0a32f568bc6 to your computer and use it in GitHub Desktop.
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
// JS | |
// Print on the console | |
console.log("Hello world!") |
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
# Ruby | |
# Print on the console | |
puts "Hello world!" |
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
// Assign a value to a variable | |
// (binding) | |
// That might be reassigned later on | |
let message = "Hello" | |
console.log(message) | |
// Reassign a value | |
message = "Olá" | |
console.log(message) | |
console.log("---=---") | |
// That will NOT be reassigned | |
const message2 = "Hello" | |
console.log(message2) | |
// CANNOT (WILL CRASH) BE REASSIGNED | |
// message2 = "Olá" | |
// console.log(message2) | |
// --------------------- | |
let age = 20 | |
console.log(age) | |
// Increment | |
// age = age + 1 | |
// age++ | |
age += 1 | |
console.log(age) | |
// Old style (DON'T USE) | |
// var message = "Hello" |
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
# Assign a value to a variables | |
message = "Hello" | |
p message | |
# Reassign a value | |
message = "Olá" | |
p message | |
age = 20 | |
p age | |
# Increment | |
# age = age + 1 | |
age += 1 | |
p age |
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
const greeting = "Hello World"; | |
// Return the length of a string | |
console.log( | |
greeting.length | |
) | |
// Get some or all elements of a string | |
console.log( | |
greeting[4] | |
) | |
// All after 2 | |
console.log( | |
greeting.substring(2) | |
) | |
// From 2 to 6 | |
console.log( | |
greeting.substring(2, 6) | |
) | |
// Upcase, downcase a string | |
greeting.length // an attribute of string | |
console.log( | |
// Is function | |
greeting.toUpperCase() | |
) | |
console.log( | |
greeting.toLowerCase() | |
) | |
// No capitalize is JS | |
// Split a string | |
console.log( | |
greeting.split(" ") | |
) | |
// Concatenate 2 strings | |
console.log( | |
"Something" + "!" | |
) | |
// Be careful with the auto casting | |
console.log( | |
1 + "2" | |
) | |
// Interpolate strings | |
const word1 = "Hello" | |
const word2 = "Again" | |
console.log( | |
`${word1} ${word2}!` | |
) |
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
greeting = "Hello World" | |
# Return the length of a string | |
p greeting.length | |
p greeting.size | |
# Get some or all elements of a string | |
p greeting[1] | |
p greeting[2..5] | |
# Upcase, downcase a string | |
p greeting.downcase | |
p greeting.upcase | |
p greeting.capitalize | |
# Split a string | |
p greeting.split # default is " " | |
greeting.split(" ") | |
# Concatenate 2 strings | |
p "Something" + "!" | |
# Be careful with the auto casting | |
# p 1 + "2" # DOES NOT WORK | |
# Interpolate strings | |
word1 = "Hello" | |
word2 = "Again" | |
p "#{word1} #{word2}!" |
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
// const or let? | |
// const fruits = ["Apple", "Orange", "Tomato", "Cupuaçu", "Bacuri"] | |
// console.log(fruits) | |
// // CRUD | |
// // Create | |
// fruits.push("Pineapple") | |
// console.log(fruits) | |
// // Read | |
// console.log(fruits[1]) | |
// // Update | |
// fruits[1] = "Banana" | |
// console.log(fruits[1]) | |
// // Delete | |
// console.log(fruits) | |
// // index, # | |
// fruits.splice(1, 1) | |
// console.log(fruits) | |
// Iterate over an array | |
const fruits = ["Apple", "Orange", "Tomato", "Cupuaçu", "Bacuri"] | |
fruits.forEach((fruit) => { | |
console.log(fruit.toUpperCase()) | |
}) | |
// Anonymous Function | |
// () => {} | |
// forEach receives a function as argument |
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
# fruits = ["Apple", "Orange"] | |
# p fruits | |
# # CRUD | |
# # Create | |
# # fruits.push("Pineapple") | |
# fruits << "Pineapple" | |
# p fruits | |
# # Read | |
# p fruits[1] | |
# # Update | |
# fruits[1] = "Banana" | |
# p fruits[1] | |
# # Delete | |
# fruits.delete_at(1) | |
# p fruits | |
# Iterate over an array | |
fruits = ["Apple", "Orange", "Tomato", "Cupuaçu", "Bacuri"] | |
fruits.each do |fruit| | |
puts fruit.upcase | |
end | |
# Block (anonymous function) | |
# do |fruit| | |
# end | |
# { |fruit| } | |
# Array#each receives a block as argument |
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
// Can you vote? in JS | |
const age = 18; | |
if(age >= 18) { | |
console.log("You can vote!") | |
} else { | |
console.log("You cannot vote!") | |
} | |
// Elsif / else if in JS | |
if(age > 18) { | |
console.log("You can vote!") | |
} else if(age === 18) { | |
console.log("You can barely vote!") | |
} else { | |
console.log("You cannot vote!") | |
} | |
// What is falsy in JS? | |
undefined | |
null | |
false | |
0 | |
'' | |
NaN | |
// Ternary operator | |
const result = age >= 18 ? "can vote" : "cannot vote" | |
console.log(result) |
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
# Can you vote? | |
age = 18 | |
if age >= 18 # truthy / nil false | |
puts "You can vote" | |
else | |
puts "You cannot vote" | |
end | |
# Elsif / else if | |
if age > 18 | |
puts "You can vote" | |
elsif age == 18 | |
puts "You can barely vote" | |
else | |
puts "You cannot vote" | |
end | |
# What is falsy? | |
nil | |
false | |
# Ternary operator | |
result = age >= 18 ? "can vote" : "cannot vote" | |
p result |
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
student = { | |
name: "Ringo", | |
age: 23 | |
} | |
p student | |
# CRUD | |
# Create | |
student[:grade] = 10 | |
p student | |
# Read | |
p student[:age] | |
# Update | |
student[:grade] = 9 | |
p student | |
# Delete | |
# student.delete(:age) | |
# p student | |
# Iterate over it | |
# keys = student.keys | |
# keys.each do |key| | |
# puts "#{key} --- #{student[key]}" | |
# end | |
student.each do |key, value| | |
puts "#{key} --- #{value}" | |
end |
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
const student = { | |
name: "Ringo", | |
age: 23 | |
} | |
console.log(student) | |
// CRUD | |
// Create | |
// student["grade"] = 10 | |
student.grade = 10 | |
console.log(student) | |
// Read | |
// console.log(student["grade"]) | |
console.log(student.grade) | |
// Update | |
// student["grade"] = 9 | |
student.grade = 9 | |
console.log(student) | |
// Delete | |
delete student["age"] | |
console.log(student) | |
// Iterate over it | |
const keys = Object.keys(student) | |
keys.forEach((key) => { | |
console.log(key, student[key]) | |
}) |
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
// In JS... | |
// Arrow function () => {} is an anonymous function... | |
// ... unless you give it a name... | |
// const myFunction = () => {} | |
// A capitalize function | |
const capitalize = (string) => { | |
const firstLetter = string[0].toUpperCase(); | |
const remainder = string.substring(1).toLowerCase(); | |
debugger | |
return `${firstLetter}${remainder}`; // firstLetter + remainder | |
} | |
console.log( | |
capitalize("jOhn") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment