Commit c67f510d authored by Raquel's avatar Raquel 💬

Initial commit

parents
function isString(string){
if (typeof string === typeof "blubber"){
console.log (true)
}
else {console.log (false)}
}
isString('hello'); // => true
isString(['hello']); // => false
isString('this is a long sentence'); // => true
isString({ a: 2 }); // => false
\ No newline at end of file
const translatePigLatin =(string) =>{
let vowels="aAeEiIoOuU"
let vowelsArray= vowels.split("")
let way="way".split('')
let ay= "ay".split("")
let stringSplit= string.split("")
if (vowelsArray.includes(string[0])===true ){
stringSplit.concat(way)
console.log(stringSplit.concat(way).join(" "));
}
else if (!vowelsArray.includes(stringSplit[0]) && !vowelsArray.includes(stringSplit[1])){
let interim=stringSplit.slice(0,2)
let interimAll=interim.concat(ay)
let newString = stringSplit.slice(2,stringSplit.length).concat(interimAll)
return newString.join(" ")
}
else
{let interim=stringSplit.slice(0,1)
let interimAll=interim.concat(ay)
let newString = stringSplit.slice(1,stringSplit.length).concat(interimAll)
return newString.join(" ")}
}
console.log(translatePigLatin("glove")); // oveglay
console.log(translatePigLatin("pig")); // igpay
const myReplace = (string, a, b)=>{
if (a.charAt(0)===a.charAt(0).toUpperCase()){
return string.replace(a, b.replace(b.charAt(0), b.charAt(0).toUpperCase()));
}
else return string.replace(a, b);
}
console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting")); // "He is Sitting on the couch"
//String.fromCharCode()
//String.prototype.charCodeAt()
const toEnglish= (string)=>{
let binArray=string.split (" ")
let outputString=""
for (var i=0; i<binArray.length; i++){
outputString+=String.fromCharCode(parseInt(binArray[i],2)).toString()
}// trnasform into UTF-16 code units (String.fromCharCode), the integer of base 2 from the array of binary (parseInt)
return outputString
}
console.log(
toEnglish(
"01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
)
); // Aren't bonfires fun!?
\ No newline at end of file
const confirmEnding=(string,a)=>{
if (string.includes(a) && string[string.indexOf(string.length-1)]=== a[a.indexOf(a.length-1)])
{
return true
}
else return false
}
console.log(confirmEnding("Open sesame", "same")); // true
\ No newline at end of file
const diffArray = (a,b)=>{
let c=a.concat(b)
let newArray=[]
for (let i=0; i<c.length; i++){
if (!b.includes(c[i]) || !a.includes(c[i])){
newArray.push(c[i])
}
}
return newArray
}
console.log(diffArray(
["andesite", "grass", "dirt", "pink wool", "dead shrub"],
["diorite", "andesite", "grass", "dirt", "dead shrub"]
)) // [ 'pink wool', 'diorite' ]
\ No newline at end of file
function isArray(array){
if (typeof array === typeof [] && Number.isInteger(array.length) ){
console.log (true)
}
else {console.log (false)}
}
// simpler way below w different method:
/*
function isArray(array){
if (Array.isArray(array) === true) ){
console.log(true)
}
else {console.log (false)}
*/
isArray('hello'); // => false
isArray(['hello']); // => true
isArray([2, {}, 10]); // => true
isArray({ a: 2 }); // => false
function areSameType(array){
for (let i=0; i<array.length ; i++){
for (let z=i+1; i<array.length; i++){
if (typeof array[i] === typeof array[z]){
return true
}
else { return false}
}
}
}
console.log(areSameType(['hello', 'world', 'long sentence'])) // => true
console.log(areSameType([1, 2, 9, 10])) // => true
console.log(areSameType([['hello'], 'hello', ['bye']])) // => false
console.log(areSameType([['hello'], [1, 2, 3], [{ a: 2 }]])) // => true
\ No newline at end of file
function longest(a, b) {
let newString = a + b
let newArray = []
for (let i = 0; i < newString.length; i++) {
if (!newArray.includes(newString[i])) {
newArray.push(newString[i])
}
}
return newArray.sort().join("")
}
console.log(longest('abcccaa', 'acddddffzzz')) // => 'abcdfz'
/*
a = 'xyaabbbccccdefww'
b = 'xxxxyyyyabklmopq'
console.log(longest(a, b))// => 'abcdefklmopqwxy'
a = 'abcdefghijklmnopqrstuvwxyz'
console.log(longest(a, a)) // => 'abcdefghijklmnopqrstuvwxyz'
*/
\ No newline at end of file
function convert(number){
let array= number.toString().split("").sort().reverse()
return array
}
console.log(convert(429563)) // => [9, 6, 5, 4, 3, 2]
console.log (convert(324)) // => [4, 3, 2]
\ No newline at end of file
let authors = ['kerouac', 'fante', 'fante', 'buk', 'hemingway', 'hornby', 'kerouac', 'buk', 'fante']
let obj={}
for (let i =0; i<authors.length; i++){
if( obj[authors[i]]){
obj[authors[i]]+=1
}
else obj[authors[i]]=1
}
console.log (obj)
\ No newline at end of file
function isCaught(string){
if (string.indexOf("C")+3 >= string.indexOf("m") ){
return true
}
else return false
}
console.log(isCaught('C.....m')) // => false
console.log(isCaught('C..m')) // => true
console.log(isCaught('..C..m') ) // => true
\ No newline at end of file
let group = {
Amy: 20,
Bill: 15,
Chris: 10
}
splitTheBill = group => {
const average = Object.values(group).reduce((total, amount, index, values) => total + (amount / values.length), 0);
return Object.keys(group).reduce((total, person) => {
total[person] = Math.round(((average - group[person])));
return total;
}, {});
}
console.log(splitTheBill(group)); // => { Amy: -5, Bill: 0, Chris: 5 }
function exp(b,n){
// direct method: return Math.pow(b,n)
// recursion method:
if (n===0){ return 1}
else return b*exp(b,n-1) //calling itself n-1 times, bc it already had one loop when reching this line for the first time
}
console.log(exp(5, 3)); // => 125
console.log(exp(2, 4)); // => 16
console.log(exp(5, 1)); // => 5
console.log(exp(6, 0)); // => 1
\ No newline at end of file
class Dish {
constructor(dish, price, ingredients) {
this.dish = dish
this.ingredients = ingredients
this.price = price
}
cost() {
let total = 10
for (let i = 0; i < this.ingredients.length; i++) {
total += this.ingredients[i].cost
}
return total
}
profit() {
return this.price - this.cost()
}
}
class Restaurant {
constructor(orders, client) {
this.orders = []
}
orderDish(dish) {
this.orders.push(dish)
//client.dishes = [dish]
}
printOrders() {
for (let i = 0; i < this.orders.length; i++) {
console.log("Order # " + i + " : " + this.orders[i])
}
}
check(client) {
this.clients = client
console.log(this.clients.name)
let sum = 0
for (let i = 0; i < this.orders.length; i++) {
/* for (let j = 0; j < this.orders[i].ingredients.length; j++) {
let sum = 0
sum += this.orders[i].ingredients[j].cost
let total = 10
total += sum
*/
sum += this.orders[i].price
console.log("Order # " + i + " : " + this.orders[i].dish + "-" +
this.orders[i].price)
}
console.log("Total: " + sum)
}
}
class Ingredient {
constructor(name, cost) {
this.name = name
this.cost = cost
}
}
const restaurant = new Restaurant();
const cheese = new Ingredient("Cheese", 5);
const pepperoni = new Ingredient("pepperoni", 6)
const dough = new Ingredient("dough", 6)
const lettuce = new Ingredient("lettuce", 6)
const tomato = new Ingredient("tomato", 6)
const pizza = new Dish('Pizza', 35, [cheese, pepperoni, dough]);
const salad = new Dish('Salad', 30, [lettuce, cheese, tomato]);
const pluto = {
name: 'Pluto',
id: 1
};
const goofy = {
name: 'Goofy',
id: 2
};
//console.log(pizza.profit())
//console.log(pizza.cost());
//restaurant.orderDish(pizza);
//restaurant.orderDish(salad);
//restaurant.printOrders()
//console.log(restaurant.check(pluto))
restaurant.orderDish(pizza, goofy);
restaurant.check(goofy);
restaurant.orderDish(pizza, pluto);
restaurant.orderDish(salad, pluto);
restaurant.check(pluto);
class Dish {
constructor(dish, price, ingredients) {
this.dish = dish
this.ingredients = ingredients
this.price = price
}
cost() {
let total = 10
for (let i = 0; i < this.ingredients.length; i++) {
total += this.ingredients[i].cost
}
return total
}
profit() {
return this.price - this.cost()
}
}
class Restaurant {
constructor(orders, client) {
this.orders = {}
}
orderDish(dish, client) {
if (this.orders[client.id]) //if this exists - ie if there is already an order in
{ return this.orders[client.id].push(dish) }
else { return this.orders[client.id] = [dish] }
}
printOrders() {
for (let i = 0; i < this.orders.length; i++) {
console.log("Order # " + i + " : " + this.orders[i])
}
}
check(client) {
console.log(client.name)
let sum = 0
<<<<<<< HEAD
// const clientOrdersArray = this.orders[client.id]; // Array of dishes for the client with id 2
for (let i = 0; i < this.orders[client.id].length; i++) {
console.log(this.orders)
console.log(client.id)
// sum += clientOrder[i].price
sum += this.orders[client.id][i].price
console.log("Order # " + i + " : " + this.orders[client.id][i].dish + "-" +
this.orders[client.id][i].price) //why can i call price here, if price is in a different class
=======
for (let i = 0; i < this.orders[client.id].length; i++) {
sum += this.orders[client.id][i].price
console.log("Order # " + i + " : " + this.orders[client.id][i].dish + "-" +
this.orders[client.id][i].price)
>>>>>>> 1c85b6462f4d4a25ef3ff10c42f5314dc087c2b0
}
console.log("Total: " + sum)
}
}
class Ingredient {
constructor(name, cost) {
this.name = name
this.cost = cost
}
}
const restaurant = new Restaurant();
const cheese = new Ingredient("Cheese", 5);
const pepperoni = new Ingredient("pepperoni", 6)
const dough = new Ingredient("dough", 6)
const lettuce = new Ingredient("lettuce", 6)
const tomato = new Ingredient("tomato", 6)
const pizza = new Dish('Pizza', 35, [cheese, pepperoni, dough]);
const salad = new Dish('Salad', 30, [lettuce, cheese, tomato]);
const pluto = {
name: 'Pluto',
id: 1
};
const goofy = {
name: 'Goofy',
id: 2
};
//console.log(pizza.profit())
//console.log(pizza.cost());
//restaurant.orderDish(pizza);
//restaurant.orderDish(salad);
//restaurant.printOrders()
//console.log(restaurant.check(pluto))
restaurant.orderDish(pizza, goofy)
<<<<<<< HEAD
=======
>>>>>>> 1c85b6462f4d4a25ef3ff10c42f5314dc087c2b0
restaurant.check(goofy);
restaurant.orderDish(pizza, pluto);
restaurant.orderDish(salad, pluto);
restaurant.check(pluto);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="/.Restaurant.js" </script> </body > </html >
\ No newline at end of file
let myMath = Object.create(Math)
myMath.randomInt = function (...args) {
if (args.length === 1) {
return Math.floor(this.random() * args[0])
}
else if (args.length === 2) {
return Math.floor(this.random() * (args[0] - args[1] + 1) + args[1])
// explnation here: https://teamtreehouse.com/community/mathfloor-mathrandom-max-min-1-min-explanation
}// also use Math.floor and not math random, otherwise we get an uneven distribution, since numbers will be rounded depending on the randomness of the decimal point of each of them
}
console.log(myMath.randomInt(5)); // 3 for example
console.log(myMath.randomInt(10)); // 10 for example
console.log(myMath.random()); // some random number between 0 and 1
console.log(myMath.round(0.5));
console.log(myMath.randomInt(10, -4)); // -3 for example
\ No newline at end of file
function myNew(pr, ...args) {
const newObj = {}
pr.apply(newObj, args) //didn't use call because call passes "name" and apply passes "this.name"
Object.setPrototypeOf(newObj, pr.prototype) //set prototype passes the methods of the prototype
return newObj
}
//apply returns "this.name"
//call returns "name"
function Cat(name, lastName) {
this.name = name;
this.lastName = lastName
this.legs = 4;
}
console.log(myNew, (1, 2, 3))
const obj = {
name: 'Markov'
}
function printName() {
console.log('Thy name is: ' + this.name);
}
printName();
function myBind(a, b) {
return function () {
a.call(b)
}
}
const boundPrint = myBind(printName, obj);
boundPrint();
\ No newline at end of file
class Event {
constructor(location, date, assistants) {
this.location = location;
this.date = date;
this.assistants = assistants;
}
printInvitation(name) {
let msg = 'Hey ' + name + '!!!\nHoping to see you in my Event on the ' + this.date.toDateString() + ' in ' + this.location + '!!!\ Cheers!!';
console.log(msg);
}
confirmAssistance() {
let isComing = console.log('Hey ' + name + '!!\nCan I count with you???');
if (isComing) {
this.assistants.push(name);
}
}
printAssistants() {
console.log(this.assistants.join('\n'));
}
}
const event1 = new Event('Zürich', new Date(2025, 11, 17), ['Llorenç', 'Colin', 'Laurent']);
event1.printInvitation("john")
//don't understand the rest of the tasks ?
\ No newline at end of file
String.prototype.reverse = function () {
return this.split("").reverse().join("")
}
console.log('hello'.reverse());
String.prototype.reverse = function () {
return this.split("").reverse().join("")
}
console.log('hello'.reverse());