Sunday, February 17, 2013
How to order objects... puzzled on the last function I somehow copied and ...
friends = {
ariane : {firstName: "Ariane",
lastName: "Bertram",
number: "696966123",
address: ["via bellezza 37", "Brasile", "53550"]},
bill : {firstName: "Bill",
lastName: "Porte",
number: "666 666666",
address: ["ta dah", "palle", "99"]},
steve : {firstName: "Steve",
lastName: "Dell'inferno",
number: "213456987",
address:["Oppa", "Gang nam", "57173"]}
};
var list = function (){
for (var names in friends){
console.log(names); }};
var search = function (name){
for (var contact in friends){
if (friends[contact].firstName===name){
console.log(friends[contact]);
return (friends[contact]);}}};
//In this exercise I quite didn't understand the function for/in, and the last part where 'friends[contact].firstName ... does it mean contact is in friends and start with firstName and I have to make it equal to name because I can give it the property of a function?!
Da Sphinx questions
//This is a game about a sphinx that you met on your path. Silly game realized with operator, if/else, switch control. Hope you enjoy.
var questionSphinx = prompt ("Hello dear, what's the thing that has 4 legs when young, 2 when adult and 3 when old?","Be careful whatever you are... Human...").toLowerCase();
switch (questionSphinx){
case 'man':
var again1 = prompt("Don't be sexist! What are you? Kid or man?","You have another chance!").toLowerCase();
var again2 = prompt("You are obviously not here with your thoughts, darling... Are you in love?","Yes or Not?").toLowerCase();
if (again1 === "kid" && again2 === "yes"){alert("Ohhh, that explains a lot, I will let you pass this time, lucky kiddo!");}
else {console.log("The Sphinx reduces you to dust before you might exhale your very last breath!");}
break;
case 'human':
var again3 = prompt("Be more specific, I will destroy you otherwise. Did you mean Man or Woman?").toUpperCase;
var again4 = prompt("Are you nice enough for me?","Yes or No!?!?").toUpperCase();
if (again3 === "WOMAN" || again4 === "YES"){alert("You may pass by me, then, you cutie-pie!");}
else {console.log("Your days strolling on this planet end here, da Sphinx is eating, chewing and spitting you!");}
break;
case 'woman':
alert("You are so right my dear, you win my game and you shall pass.");
break;
default:
console.log("That is not an acceptable answer, now the Sphinx will have to kill you by tearing your body apart in pieces.");
}
How to kill a dragoon!
//Another fun Codecademy exercise to learn using variables and while loops and if/else statements.
var slaying = true;
var youHit = Math.floor(Math.random()*2);
var damageThisRound = Math.floor(Math.random()*5 +1);
var totalDamage = 0;
while (slaying){
if (youHit===1){
totalDamage+=damageThisRound;
if (totalDamage>=4){console.log("Congratulations! You are by far the best reptiles'slayer, you bring back home the dragoon's treasure.");}
else {youHit=Math.floor(Math.random()*2);
slaying=false;}
console.log("You spanked da dragoon very well with your spear!!!");}
else {console.log("Da dragoon just flayed you alive, your skin will be used for crafting its winter coat.");}
slaying=false;
}
Find and spell your name!
//Hello, this is a code which took me some time to really understand while learning: it allows you to find your name in a text and spell it word by word as many times it is written in the text. I had to use 3 variables for text, myName and hits. hits (by the function .push) allows to move from letter to letter, till your name in the text won't be found anymore. Capital letters count. This is another course realized in Codecademy by Eric Weinstein.
var text ="gabriele gabriele Gabriele gabriele and Gabriele";
var myName = "Gabriele";
var hits = [];
for (var i=0;i<text.length;i++){
if (text[i]==="G"){
for (var j=i; j<(myName.length+i);j++){
hits.push(text[j]);} } }
if (hits===0){console.log("Your name wasn't found!");}
else {console.log(hits);}
var text ="gabriele gabriele Gabriele gabriele and Gabriele";
var myName = "Gabriele";
var hits = [];
for (var i=0;i<text.length;i++){
if (text[i]==="G"){
for (var j=i; j<(myName.length+i);j++){
hits.push(text[j]);} } }
if (hits===0){console.log("Your name wasn't found!");}
else {console.log(hits);}
Saturday, February 16, 2013
Rock, paper, scissors game in JS.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice <0.34){computerChoice = "rock";}
else if(computerChoice <0.67){computerChoice = "paper";}
else{computerChoice = "scissors";}
var compare = function (choice1, choice2){
if (choice1===choice2){
return "The result is a tie!";}
else if(choice1==="rock"){
if (choice2==="scissors"){return "rock wins";}
else {return "paper wins";}}
else if (choice1==="paper"){
if (choice2==="rock"){return "paper wins";}
else {return "scissors wins";}}
else if (choice1==="scissors"){
if(choice2==="rock"){return "rock wins";}
else {return "scissors wins";}}};
compare(userChoice, computerChoice);
Batman beats you! JS.
confirm("Are you ready to play?");
var age = 23;
if (age <= prompt("what is your age?")){
console.log("Let's the show begin!");}
else
{console.log("You may continue, but be aware!");}
console.log("Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.");
console.log("Batman glares at you.");
var userAnswer=prompt("Are you feeling lucky, punk?");
if (userAnswer === "YES" )
{console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");}
else
{console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");}
var feedback = prompt("How did I do from 1 to 10?");
if (feedback>8)
{console.log("This is just the beginning of my game empire. Stay tuned for more!");}
else
{console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");}
//This is the first game I finished with the big help of Codecademy.
FizzBuzz Exercise From Codecademy. Control flow solution.
for (i=1; i<=20; i++){
if (i % 3 === 0){
if (i % 5 === 0){console.log ("FizzBuzz");}
else if (i % 3 === 0) console.log("Fizz");
else {console.log (i);}}
else if (i % 5 === 0) {console.log("Buzz");}
else {console.log(i);}
}
//Exercise FizzBuzz with control flow for loop and statements if/else. It will print out 'Fizz' for multiples of 3, 'Buzz' for 5 and 'FizzBuzz' for both. This is how I see the solution. Still learning in Codecademy.
Subscribe to:
Comments (Atom)