Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created February 9, 2020 23:16
Show Gist options
  • Save rodloboz/40b47d645d8d3a1013a4f5fe8f254c96 to your computer and use it in GitHub Desktop.
Save rodloboz/40b47d645d8d3a1013a4f5fe8f254c96 to your computer and use it in GitHub Desktop.
JS Basics
console.log('Hello, world!')
"Hello" // String
'Hello' // String
3 // Number
3.14 // Number
console.log(typeof(3.14))
true // Boolean
false // Boolean
// Base: 0, 1, 2, 3, 4, 5... 10
// 10 - Decimal
let number = Number.parseInt("34", 10)
console.log(typeof(number))
console.log(parseInt("lkhkdhalfklkfjlkj"))
// NaN - Not a Number
null // Representes the absense of a value
undefined // means that a variable or function is not defined
console.log(5 / 4)
puts "Hello, world!"
"Hello" # String
'Hello' # String
3 # Integer
3.14 # Float
puts 3.14.class
true # TrueClass (Boolean)
false # FalseClass (Boolean)
number = "34".to_i
p number.class
nil # represents the absence of a value
puts "hkahfkhlkaflk".to_i
puts 5 / 4
// Array CRUD
// Create
//. 0 1
const rappers = ['Jay Z', ' Nas']
// Read
console.log(rappers[0])
// Update
rappers.push('Eminem')
console.log(rappers)
rappers[0] = 'Jay Electronica'
console.log(rappers)
// Delete
// index, no. of el
rappers.splice(-1)
console.log(rappers)
// Splice takes an index an
// an optional number of elements to
// delete
// rappers.splice(0, 2)
const beatles = ['John', 'Paul', 'Ringo', 'George']
// ES6 Syntax
beatles.forEach((beatle) => {
console.log(beatle.toUpperCase())
})
# Array CRUD
# Create
#. 0 1
rappers = ["Jay Z", "Nas"]
# Read
puts rappers[0]
# Update
# rappers << "Eminem"
rappers.push("Eminem")
p rappers
rappers[0] = "Jay Electronica"
p rappers
# Delete
rappers.delete_at(-1)
p rappers
beatles = ["John", "Paul", "Ringo", "George"]
beatles.each do |beatle|
puts beatle.upcase
end
// Objects key => value pairs
// Objects => Hash in Ruby
// Create
const student = {
firstName: 'John',
middleName: 'K.',
lastName: 'Smith'
}
console.log(student)
// Read
console.log(student['firstName'])
// Accepts dot. notation
console.log(student.firstName)
// Update
// student['firstName'] = 'Peter'
student.firstName = 'Peter'
console.log(student.firstName)
// Delete
delete(student.middleName)
console.log(student)
// Iteration
Object.keys(student).forEach((key) => {
console.log(`The value of ${key} is ${student[key]}.`)
});
# Objects key => value pairs
# Hash => Object in JS
# Create
student = {
first_name: "John",
middle_name: "K.",
last_name: "Smith"
}
# Read
puts student[:first_name]
# Update
student[:first_name] = "Peter"
puts student[:first_name]
# Delete
student.delete(:middle_name)
p student
# Iteration
student.each do |key, value|
puts "The value of #{key} is #{value}."
end
// Varaibles
// Old JS
var counter = 1
console.log(counter)
counter += 1
// counter = counter + 1
console.log(counter)
// Declare a vsriable without assignment
// var counter
// => undefined
var firstName = "Rui"
// New JS
// const newCounter = 1
// console.log(newCounter)
// newCounter += 1
// TypeError: Assignment to constant variable.
// const variables cannot be reassigned
let newCounter = 1
newCounter += 1
console.log(newCounter)
# Variables
# Ruby you declare and assign
counter = 1
puts counter
counter += 1
# counter = counter + 1
puts counter
first_name = "Rui"
firstName = "Rui"
lastName = "Freitas"
// Old JS (concatenation)
// var sentence = firstName + ' ' + lastName
// console.log(sentence)
// ES6 Js - Template literals
const sentence = `${firstName} ${lastName}`
console.log(sentence)
first_name = "Rui"
last_name = "Freitas"
puts "Hello, #{first_name} #{last_name}"
const age = 17
if (age >= 18) {
console.log('You can vote!')
}
else {
console.log("You're too young! Go home")
}
// Ternary operator
const raining = false
const accessory = raining ? 'umbrella' : 'sunglasses'
console.log(accessory)
// JS does type coersion
// careful with == comparisons
const digit = '0'
if (digit === 0) {
console.log('Zero')
} else if (digit === 1) {
console.log('One')
} else {
console.log('Sorry, I dont know that number')
}
age = 18
if age >= 18
puts "You can vote!"
else
puts "You're too young! Go home!"
end
# Ternary operator
raining = false
accessory = raining ? "umbrella" : "sunglasses"
puts accessory
digit = "0"
if digit == 0
puts "Zero"
elsif digit == 1
puts "One"
else
puts "Sorry, I don't know that number!"
end
// Methods are called functions
// Old JS
function greeting(name = 'Stranger') {
return 'Hello, ' + name + '!'
}
// In JS parenthesis are not optional
console.log(greeting())
console.log(greeting('John'))
// ES6 syntax
// Arrow functions: () => {}
const newGreeting = (name = 'Stranger') => {
return `Hello, ${name}!`
}
console.log(newGreeting());
console.log(newGreeting('Mike'))
const capitalize = (string) => {
return string[0].toUpperCase() + string.slice(1, string.length).toLowerCase()
}
console.log(capitalize('sUSAn'))
# Method declaration
def greeting(name = "Stranger")
# return "Hello, #{name}!"
# Ruby has implicit returns
"Hello, #{name}!"
end
puts greeting # greeting() optional
puts greeting("John")
def myCapitalize(string)
# Grab the first letter
# upcase thr first letter
# Grab the rest
# Downcase the rest
# join them together
string[0].upcase + string[1..-1].downcase
end
puts myCapitalize("sUsAN")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment