Last active
December 12, 2015 02:29
-
-
Save marioplumbarius/4699407 to your computer and use it in GitHub Desktop.
Programinhas que escrevo enquanto estudo JavaScript.
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
/* | |
Exercice: | |
Define a function called olderAge. We want the function to return the age of the person who is older. | |
*/ | |
// Our person constructor | |
function Person (name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
// We can make a function which takes persons as arguments | |
// This one computes the difference in ages between two people | |
var ageDifference = function(person1, person2) { | |
return person1.age - person2.age; | |
}; | |
// Make a new function, olderAge, to return the age of | |
// the older of two people | |
var olderAge = function(person1, person2){ | |
var ages = [person1.age, person2.age]; | |
var maxAge = 0; | |
for(var i = 0; i<ages.length;i++){ | |
if(ages[i] > maxAge){ | |
maxAge += ages[i]; | |
} | |
} | |
return maxAge; | |
}; | |
// Let's bring back alice and billy to test our new function | |
var alice = new Person("Alice", 30); | |
var billy = new Person("Billy", 25); | |
console.log("The older person is "+olderAge(alice, billy)); |
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
/* | |
Exercício: Crie uma caixa registradora. | |
*/ | |
/* Caixa registradora */ | |
var cashRegister = { | |
// valor total da compra | |
total:0, | |
// valor da última transação feita | |
lastTransactionAmount: 0, | |
// adiciona itens a compra | |
add: function(itemCost){ | |
this.total += itemCost; | |
this.lastTransactionAmount = itemCost; | |
}, | |
// calcula o valor bruto de cada produto, de acordo com a quantidade | |
scan: function(item,quantity){ | |
switch (item){ | |
case "eggs": | |
this.add(0.98 * quantity); | |
break; | |
case "milk": | |
this.add(1.23 * quantity); | |
break; | |
case "magazine": | |
this.add(4.99 * quantity); | |
break; | |
case "chocolate": | |
this.add(0.45 * quantity); | |
break; | |
} | |
return true; | |
}, | |
// cancela a última transação | |
voidLastTransaction : function(){ | |
this.total -= this.lastTransactionAmount; | |
this.lastTransactionAmount = 0; | |
}, | |
// fornece desconto para os funcionários | |
applyStaffDiscount: function(employee){ | |
this.total -= (this.total * employee.discountPercent / 100); | |
} | |
}; | |
/* Constructor para adicionar funcionários que tem desconto nos produtos */ | |
function StaffMember(name,discountPercent){ | |
this.name = name; | |
this.discountPercent = discountPercent; | |
} |
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
/* | |
Exercice: Create a dragon slaying minigame using while loops and if/else statements | |
*/ | |
var slaying = true; | |
var youHit = Math.random(); // random number from 0 to 1; | |
var damageThisRound = Math.floor(Math.random() * 6 + 1); // random number from 1 to 6 | |
var totalDamage = 0; | |
while(slaying){ | |
if(youHit === 1){ | |
console.log("Yeah baby. You rocket it!"); | |
totalDamage += damaThisRound; | |
if(totalDamage >= 4){ | |
console.log("You killed the dragon!"); | |
slaying = false; | |
} else{ | |
youHit = Math.random(); | |
slaying = false; | |
} | |
}else{ | |
console.log("Oh man. The dragon defeated you."); | |
} | |
slaying = false; | |
} |
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
/* | |
Exercice: Print out the numbers from 1 - 20. | |
The rules: | |
1. For numbers divisible by 3, print out "Fizz". | |
2. For numbers divisible by 5, print out "Buzz". | |
3. For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console. | |
4. Otherwise, just print out the number. | |
PS: Try answering this exercise using a nested conditional. | |
*/ | |
for(var x = 1; x<21; x++){ | |
if(x % 5 === 0){ | |
if(x % 3 === 0){ | |
console.log('FizzBuzz'); | |
}else{ | |
console.log('Buzz'); | |
} | |
} else if(x % 3 === 0){ | |
console.log('Fizz'); | |
}else{ | |
console.log(x); | |
} | |
} |
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
/* | |
Exercice: Imagine you have a movie collection and you want to write some code that assigns your review to each of them. | |
Instructions: Below are the movies and your review. Use a switch structure to write code for the information below: | |
1. "Matrix" - "good trip out" | |
2. "Princess Bride" - "awesome date night movie" | |
3. "Welcome to America" - "Amjad's favorite" | |
4. "Remember the Titans" - "love the sports" | |
5. "Why do I look like I'm 12?" - "The Ryan and Zach story" | |
6. "Fighting Kangaroos in the wild" - "Token Australian movie for Leng" | |
PS: 'getReview' should be a function that takes a movie name and returns its review based on the information above. | |
If given a movie name not found just return "I don't know!". | |
*/ | |
var getReview = function(movie){ | |
switch(movie){ | |
case 'Matrix': | |
return "good trip out"; | |
case 'Princess Bride': | |
return "awesome date night movie"; | |
case 'Welcome to America': | |
return "Amjad's favorite"; | |
case 'Remember the Titans': | |
return "love the sports"; | |
case 'Why do I look like I\'m 12?': | |
return "The Ryan and Zach story"; | |
case 'Fighting Kangaroos in the wild': | |
return "Token Australian movie for Leng"; | |
default: | |
return "I don\'t know!"; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment