Personal Javascript
function printInfo(msg) {
console.log(typeof msg + ";", msg);
}
printInfo(5);
printInfo("apples");
printInfo(["Bob", "Joe", "Kaiden"]);
printInfo(5*3)
function Drink(name, color, calories) {
this.name = name;
this.color = color;
this.calories = calories;
this.type = "";
}
Drink.prototype.setType = function(type) {
this.type = type;
}
Drink.prototype.toJSON = function() {
const data = {name: this.name, color: this.color, calories: this.calories, type: this.type};
const jsonString = JSON.stringify(data);
return jsonString;
}
var water = new Drink("Water", "Clear", 0);
water.setType("Water");
printInfo(water);
printInfo(water.toJSON());
var soda = [
new Drink("Fanta", "Orange", 160),
new Drink("Mtn Dew", "Green", 170),
new Drink("Root Beer", "Brown", 180),
new Drink("Coca-Cola", "Brown", 140),
new Drink("Pepsi", "Brown", 150),
new Drink("Sprite", "Clear", 140)
]
var juice = [
new Drink("Apple Juice", "Gold-Yellow", 180),
new Drink("Orange Juice", "Orange-Yellow", 110),
new Drink("Cranberry Juice", "Red", 100),
]
function Drinks(water, soda, juice) {
water.setType("Water");
this.water = water;
this.drinks = [water];
this.soda = soda;
this.juice = juice;
this.soda.forEach(soda => {soda.setType("Soda"); this.drinks.push(soda);});
this.juice.forEach(juice => {juice.setType("Juice"); this.drinks.push(juice);});
this.jsonDrinks = [];
this.drinks.forEach(drink => this.jsonDrinks.push(drink.toJSON()));
}
pantry = new Drinks(water, soda, juice);
printInfo(pantry.drinks);
printInfo(pantry.drinks[7].name);
printInfo(pantry.jsonDrinks[7]);
printInfo(JSON.parse(pantry.jsonDrinks[7]));
var a = 1;
var b = 1;
var c = 1;
var range = 20;
console.log("Here are some Pythagorean Triples:")
for(let i = 0; i < range; i++) {
for(let i = a; i < 500; i++) {
b = i;
c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
if (Math.floor(c)==c){
console.log(a,b,c);
}
}
a++;
}