<a href="javascript:void(0)"></a>
<script>
console.log(1 + 1);
</script>
<script src="assets/js/filename.js"></script>
$ git config --global user.name "Marco Almeida"
$ git config --global user.email "marcoantonio@infojr.com.br"
$ ssh-keygen -t rsa -C "marcoantonio@infojr.com.br"
// Integer
13;
// Float
9.81;
// Exponent
2.998e8;
// Arithmetic
100 + 4 * 11;
// Infinity
Infinity - 1;
// Not a Number
NaN;
0/0;
typeof(4);
// number
"Patch my boat with chewing gum";
'Monkeys wave goodbye';
// Concatenate
"con" + "cat" + "e" + "nate";
typeof('This is the first line\nAnd this is the second');
//string
3 > 2;
// true
3 < 2;
// false
"Aardvark" < "Zoroaster";
// true
"Itchy" !== "Scratchy";
// false
NaN === NaN;
// false
true && false;
// false
true || false;
// true
true ? 1 : 2;
// 1
typeof(true);
// boolean
var a;
null;
undefined;
typeof(undefined);
// undefined
var dieRoll = function () {
return Math.ceil(Math.random() * 6);
};
var age = parseInt(window.prompt('What\'s your age?'));
console.log(dieRoll());
typeof(dieRoll);
// function
var array = [0, 'Marc', {}, 1, 'James', {}];
var object = { name: 'Marc', strength: 3 };
var date = new Date();
typeof(array);
// object
(parseInt(prompt()) > parseInt(prompt())) ? 'First' : 'Second';
var strength = 5,
firePower;
firePower = '3';
firePower = 10;
console.log(firePower);
alert('Welcome to Save the Princess!');
var name = prompt('What is your name?');
console.log(name);
var humanize = function (name) {
return name[0].toUpperCase() + name.slice(1);
};
confirm('Is your name ' + humanize(name) + '?');
var strength = prompt('What\'s your strength?');
if (strength > 3) {
console.log('Very strong!');
} else if (strength > 1) {
console.log('Human.');
} else {
console.log('Weak...');
}
switch (parseInt(strength)) {
case 4:
case 5:
console.log('Very strong!');
break;
case 2:
case 3:
console.log('Human.');
break;
case 1:
console.log('Weak...');
}
var strength = 3, counter = 0, totalDamage = 0;
while (counter < strength) {
totalDamage += Math.ceil(Math.random() * 6);
counter++;
}
console.log(totalDamage);
counter = 0;
totalDamage = 0;
do {
totalDamage += Math.ceil(Math.random() * 6);
counter++;
} while (counter <= strength)
console.log(totalDamage);
totalDamage = 0;
for (counter = 0; counter < strength; counter += 1) {
totalDamage += Math.ceil(Math.random() * 6);
}
console.log(totalDamage);
totalDamage = strengthOne * random(1, 6) - armorTwo * random(1, 6);
var strength1 = Math.ceil(Math.random() * 5),
armor1 = Math.ceil(Math.random() * 5),
resistance1 = Math.ceil(Math.random() * 5),
hP1 = resistance1 * 5,
currentHP1 = hP1,
strength2 = Math.ceil(Math.random() * 5),
armor2 = Math.ceil(Math.random() * 5),
resistance2 = Math.ceil(Math.random() * 5),
hP2 = resistance2 * 5,
currentHP2 = hP2,
damage = 0;
while (currentHP1 > 0 && currentHP2 > 0) {
currentHP2 -=
Math.max(0, strength1 * (Math.ceil(Math.random() * 6) + 1)
- armor2 * (Math.ceil(Math.random() * 6)));
if (currentHP2 > 0) {
currentHP1 -=
Math.max(0, strength2 * (Math.ceil(Math.random() * 6) + 1)
- armor1 * (Math.ceil(Math.random() * 6)));
}
}
if (currentHP1 > 0) {
console.log('* First\nStrength: ', strength1, '\nArmor: ', armor1, '\nResistance: ', resistance1, '\nHP: ', currentHP1, '/', hP1);
console.log('\n\nSecond\nStrength: ', strength2, '\nArmor: ', armor2, '\nResistance: ', resistance2, '\nHP: ', currentHP2, '/', hP2);
} else {
console.log('\n\nFirst\nStrength: ', strength1, '\nArmor: ', armor1, '\nResistance: ', resistance1, '\nHP: ', currentHP1, '/', hP1);
console.log('* Second\nStrength: ', strength2, '\nArmor: ', armor2, '\nResistance: ', resistance2, '\nHP: ', currentHP2, '/', hP2);
}
var rollDie = function (sides) {
return Math.ceil(Math.random() * sides);
};
function rollDie(sides) {
return Math.ceil(Math.random() * sides);
}
rollDie();
var rollDie = function (sides) {
'use strict';
return Math.ceil(Math.random() * (sides || 6));
};
var strength = 3,
armor = 5,
attack = function (strength, armor) {
'use strict';
return (strength * rollDie(6)) - (armor * rollDie(6));
};
attack(strength, armor, 5, 6);
var strength = 1,
newStrength = function () {
'use strict';
var strength = 4;
};
newStrength();
console.log(strength);
// => 1
var strength = 1,
newStrength = function () {
'use strict';
strength = 4;
};
newStrength();
console.log(strength);
// => 4
var strength1 = 1,
armor1 = 2,
hp1 = 5,
strength2 = 2,
armor2 = 1,
hp2 = 5,
battle = function (strength1, armor1, hp1, strength2, armor2, hp2) {
'use strict';
var attack = function (strength) {
return strength * dieRoll();
},
defend = function () {
return armor * dieRoll();
},
dieRoll = function () {
return Math.ceil(Math.random() * 6) ;
};
while (hp1 > 0 && hp2 > 0) {
hp2 -= Math.max(attack(strength1) - defend(armor2), 0);
if (hp2 > 0) {
hp1 -= Math.max(attack(strength2) - defend(armor1), 0);
}
}
return (hp1 <= 0) ? [strength2, armor2, hp2] : [strength1, armor1, hp1];
};
battle(strength1, armor1, hp1, strength2, armor2, hp2);
var dieRoll = function (sides) {
'use strict';
if (sides === undefined) {
sides = 6;
}
return Math.ceil(Math.random() * sides);
};
var dieRoll = function (sides) {
'use strict';
return Math.ceil(Math.random() * (sides || 6));
};
var randomCharacter = function (name, limit) {
'use strict';
var strength = 0,
skill = 0,
resistance = 0,
armor = 0,
firePower = 0,
randomStatus = function () {
return Math.floor(Math.random() * 6);
};
if (limit >= 0) {
strength = Math.min(generateStatus(), limit);
limit -= strength;
}
if (limit >= 0) {
skill = Math.min(generateStatus(), limit);
limit -= skill;
}
if (limit >= 0) {
resistance = Math.min(generateStatus(), limit);
limit -= resistance;
}
if (limit >= 0) {
armor = Math.min(generateStatus(), limit);
limit -= armor;
}
if (limit >= 0) {
firePower = Math.min(generateStatus(), limit);
limit -= firePower;
}
return [name, strength, skill, resistance, armor, firePower];
}
Representação de um conjunto de valores
var character = ['Marc', 1, 4, 2, 3, 0],
monster = ['Bat', 1, 2, 2, 1, 0];
character[0];
// => 'Marc'
var character = ['Marc', 1, 4, 2, 3, 0];
character.length;
character['length'];
var character = ['Marc', 1, 4, 2, 3, 0];
character.push(character[4] * 5);
// => 7
character.pop();
// => 15
var character = {
name: 'Marc',
strength: 1,
skill: 4,
armor: 2,
resistance: 3,
firePower: 0
};
character.name;
// => "Marc"
character['strength'];
// => 1
character.healthPoints = character.resistance * 5;
// => 15
var character = {
name: 'Marc',
strength: 1,
skill: 4,
armor: 2,
resistance: 3,
firePower: 0
};
character.name;
// => "Marc"
character['strength'];
// => 1
character.healthPoints = character.resistance * 5;
// => 15
var characters = [
{ name: 'Marc', strength: 1, skill: 4, armor: 2, resistance: 3, firePower: 0 },
{ name: 'James', strength: 1, skill: 3, armor: 1, resistance: 1, firePower: 4 },
{ name: 'Leon', strength: 2, skill: 3, armor: 2, resistance: 1, firePower: 2 }],
strongestCharacter = function (characters) {
var strongest = characters[0],
index;
for (index = 0; index < characters.length; index += 1) {
if (strongest.strength < characters[index].strength) {
strongest = characters[index];
}
}
return strongest;
};
strongestCharacter(characters);
var Die = {
roll: function () {
return Math.ceil(Math.random() * 6);
}
};
Die.roll();
// => 5
var Die = {
roll: function () {
return Math.ceil(Math.random() * 6);
}
},
attack = function () {
return this.strength * Die.roll();
},
defend = function () {
return this.armor * Die.roll();
},
marc = { name: 'Marc', strength: 1, skill: 4, armor: 2, resistance: 3, firePower: 0, attack: attack, defend: defend },
james = { name: 'James', strength: 1, skill: 3, armor: 1, resistance: 1, firePower: 4 };
window.attack();
// => NaN
james.attack = attack;
james.defend = defend;
james.currentHealthPoints = james.resistance * 5;
james.currentHealthPoints -= Math.max(marc.attack() - james.defend(), 0);
var Die = {
roll: function () {
'use strict';
return Math.ceil(Math.random() * 6);
}
},
randomCharacter = function (name, limit) {
'use strict';
var character = {
name: name,
attack: function () {
return Die.roll() * Math.max(this.strength, this.firePower);
},
defend: function () {
return Die.roll() * this.armor;
}
},
randomStatus = function () {
return Math.floor(Math.random() * 6);
};
if (limit >= 0) {
character.strength = Math.min(randomStatus(), limit);
limit -= character.strength;
}
if (limit >= 0) {
character.skill = Math.min(randomStatus(), limit);
limit -= character.skill;
}
if (limit >= 0) {
character.resistance = Math.min(randomStatus(), limit);
limit -= character.resistance;
}
if (limit >= 0) {
character.armor = Math.min(randomStatus(), limit);
limit -= character.armor;
}
if (limit >= 0) {
character.firePower = Math.min(Math.min(randomStatus(), limit), 5);
limit -= character.firePower;
}
character.healthPoints = character.resistance * 5;
return character;
},
battle = function (character, opponent) {
var winner;
while (character.healthPoints > 0 && opponent.healthPoints > 0) {
opponent.healthPoints -= character.attack() - opponent.defend();
if (opponent.healthPoints > 0) {
character.healthPoints -= opponent.attack() - character.defend();
}
}
winner = (character.healthPoints > 0) ? character : opponent
return winner;
};
marc = randomCharacter('Marc', 10);
james = randomCharacter('James', 10);
battle(marc, james);
var Character = function (attrs) {
this.strength = attrs.strength;
this.skill = attrs.skill;
// ...
};
var marc = new Character({ strength: 2, skill: 4 });
Character.prototype.attack = function () {
// ...
};
var marc = new Character({ strength: 2, skill: 4 });
marc.attack();
var Character = function (attrs) {
this.strength = attrs.strength;
this.armor = attrs.armor;
this.firePower = attrs.firePower;
};
Character.prototype.attack = function () {
// ...
};
Character.prototype.defend = function () {
// ...
};
Marc
3
<script>
var character = document.querySelector('div'),
divs = document.querySelectorAll('div');
console.log(character.querySelector('.name').innerText);
console.log(character.querySelector('.character > img').src);
console.log(character.getElementById('strongest'));
</script>
<div class="character">
<img src="link-to-img" alt="Character profile">
<div class="name">Marc</div>
<div class="strength" id="strongest">3</div>
</div>
<script>
var character = document.querySelector('div'),
divs = document.querySelectorAll('div'),
name = character.querySelector('.name');
strength = character.querySelector('.strength');
character.insertBefore(strength, name);
name.innerHTML = 'Marc';
</script>
<div class="character"></div>
<script>
var character = document.querySelector('.character'),
node = document.createElement('div');
text = document.createTextNode('Marc');
character.appendChild(node);
node.appendChild(text);
</script>
<div class="character"></div>
<script>
var character = document.querySelector('.character');
character.setAttribute('id', 'character_1');
character.getAttribute('id');
character.style.color = '#333';
</script>
addEventListener('click', function () {
alert('You clicked!);
});
var button = document.querySelector('button');
button.addEventListener('click', function () {
console.log('Character 1 attacks Character 2');
});
var link = document.querySelector('a');
link.addEventListener('click', function (event) {
event.preventDefault();
console.log('Character 1 attacks Character 2');
});
<div style="padding: 20px;">
<button>Click me!</button>
<div>
<script>
var div = document.querySelector('div'), button = document.querySelector('button');
button.addEventListener('click', function (event) {
alert('I was clicked!');
});
div.addEventListener('click', function (event) {
alert('I was clicked!!');
});
</script>
var keyboardComponent = function (event) {
swtich(event.which) {
case 13: // Enter
select();
break;
case 38: // Up
selectUp();
break;
case 40: // Down
selectDown();
break;
}
};
window.addEventListener('keyup', keyboardComponent);
window.addEventListener('load', function () {
alert('Page is loaded');
});
document.body.style.background = 'blue';
setTimeout(function () {
document.body.style.background = 'yellow';
}, 2000);
<form action="new.html" method="GET" id="new_character">
<input type="text" name="name" value="" placeholder="">
<input type="submit" value="Criar">
</form>
<script>
var form = document.querySelector('#new_character');
form.addEventListener('submit', function (event) {
event.preventDefault();
console.log(this.firstElementChild.name);
console.log(this.firstElementChild.value);
});
</script>
GET /index.html HTTP/1.1
Host: save-the-princess.com
User-Agent: Google Chrome
HTTP/1.1 200 OK
Content-Length: 3122
Content-Type: text/html
Last-Modified: Wed, 19 Dez 2014 10:48:09 GMT
... the rest of the document”
GET /index.html?name=Marco&strength=3 HTTP/1.1
Host: save-the-princess.com
User-Agent: Google Chrome
POST /index.html HTTP/1.1
Host: save-the-princess.com
User-Agent: Google Chrome
name=Marc&strength=3
var request = new XMLHttpRequest();
request.open('GET', 'new.html', false);
request.send();
console.log(request.status);
console.log(request.statusText);
console.log(request.responseText);
var request = new XMLHttpRequest();
request.open('GET', 'new.html', true);
request.addEventListener('load', function () {
// Code here...
});
request.send();
// enemies.json
[{ "name": "Skeleton", "strength": 2 }, { "name": "Bat", "strength": 1 }, { "name": "Pikachu", "strength": 5 }]
// app.js
var request = new XMLHttpRequest();
request.open('GET', 'enemies.json', true);
request.addEventListener('load', function () {
console.log(JSON.parse(this.responseText));
});
request.send();
HAVERBEKE Martin. Eloquent JavaScript: A modern introduction to JavaScript.