Skip to content

Instantly share code, notes, and snippets.

View sevperez's full-sized avatar

Severin Perez sevperez

View GitHub Profile
var someValue = "Hello, world!";
console.log("Type of someValue:", typeof someValue);
// Logs: Type of someValue: string
someValue = 2018;
console.log("Type of someValue:", typeof someValue);
// Logs: Type of someValue: number
someValue = {};
console.log("Type of someValue:", typeof someValue);
console.log("\"string\" type:", typeof "string"); // Logs: "string" type: string
console.log("7 type:", typeof 7); // Logs: 7 type is: number
console.log("7.5 type:", typeof 7.5); // Logs: 7.5 type is: number
console.log("true type:", typeof true); // Logs: true type: boolean
console.log("undefined type:", typeof undefined); // Logs: undefined type: undefined
function isPalindrome(str) {
var reverse = str.split("").reverse().join("");
return str === reverse;
}
function getCharCounts(chars) {
var charCounts = {};
for (var i = 0, charLen = chars.length; i < charLen; i += 1) {
if (charCounts[chars[i]]) {
charCounts[chars[i]] += 1;
function Robot(name, job) {
this.name = name;
this.job = job;
}
Robot.prototype.introduce = function() {
console.log("Hi! I'm " + this.name + ". My job is " + this.job + ".");
};
var bender = new Robot("Bender", "bending");
function makeCalendar(name) {
var calendar = {
owner: name,
events: [],
};
return {
addEvent: function(event, dateString) {
var eventInfo = {
event: event,
function schedulerMaker(name) {
return function(event) {
return function() {
console.log(event + " with " + name + ".");
};
};
}
var adaScheduler = schedulerMaker("Ada");
var coffeeWithAda = adaScheduler("Coffee");
function makeEventDescriber(event, date) {
return function() {
console.log(date + ": " + event);
};
}
var coffeeWithAda = makeEventDescriber("Coffee with Ada.", "8/1/2018");
var partyAtCharles = makeEventDescriber("Party at Charles' house.", "8/4/2018");
coffeeWithAda(); // Logs: "8/1/2018: Coffee with Ada."
var event = "Coffee with Ada.";
function describeEvent() {
console.log(event);
}
function calendar() {
var event = "Party at Charles' house.";
describeEvent();
var event = "Coffee with Ada.";
function calendar() {
var event = "Party at Charles' house.";
console.log(event);
}
calendar(); // Logs: "Party at Charles' house."
function Robot(name, job) {
this.name = name;
this.job = job;
this.introduce = function() {
console.log("Hi! I'm " + this.name + ". My job is " + this.job + ".");
};
}
function AI(name, job, intelligenceLevel) {