-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomework.js
146 lines (102 loc) · 3.51 KB
/
Homework.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Chapter 06: Create your first objects
//Modeling a bank account
//Write a program that creates an account object with the following characteristics:
//A name property set to "Alex".
//A balance property set to 0.
// A credit method adding the (positive or negative) value passed as an argument to the account balance.
// A describe method returning the account description.
// Use this object to show its description, crediting 250, debiting 80, then show its description again.
const account = {
name: 'Alex',
balance: 0,
credit(value) {
this.balance += value;
},
describe() {
return `owner: ${this.name}, balance: ${this.balance}`;
}
};
console.log(account.describe());
account.credit(250);
account.credit(-80);
console.log(account.describe());
// Adding character experience
// Improve our example RPG program to add an experience property named xp to the character. Its initial value is 0. Experience must appear in character description.
const aurora = {
name: "Aurora",
health: 150,
strength: 25,
xp: 0,
// Return the character description
describe() {
return `${this.name} has ${this.health} health points, ${this
.strength} as strength and ${this.xp} XP points`;
}
};
aurora.health -= 20;
aurora.strength += 10;
aurora.xp += 15;
console.log(aurora.describe());
// Modeling a dog
// Complete the following program to add the dog object definition.
const dog = {
name: 'Fang',
species: 'boarhound',
size: 75,
bark() {
return "Grrr! Grrr!";
}
};
console.log(`${dog.name} is a ${dog.species} dog measuring ${dog.size}`);
console.log(`Look, a cat! ${dog.name} barks: ${dog.bark()}`);
//Chapter 07: Store data in arrays
// Musketeers
// Write a program that:
// Creates an array named musketeers containing values "Athos", "Porthos" and "Aramis".
// Shows each array element using a for loop.
// Adds the "D'Artagnan" value to the array.
// Shows each array element using the forEach() method.
// Remove poor Aramis.
// Shows each array element using a for-of loop.
const musketeers = ["Athos", "Porthos", "Aramis"];
for (let i = 0; i < musketeers.length; i++) {
console.log(musketeers[i]);
}
musketeers.push("D'Artagnan");
musketeers.forEach(musketeer => {
console.log(musketeer);
});
musketeers.splice(2, 1);
for (const musketeer of musketeers) {
console.log(musketeer);
}
//Sum of values
// Write a program that creates the following array, then calculates and shows the sum of its values (42 in that case).
// const values = [3, 11, 7, 2, 9, 10];
const values = [3, 11, 7, 2, 9, 10];
let sum = 0;
for (let i = 0; i < values.length; i++) {
sum += values[i];
}
console.log(sum);
//Chapter 08: Work with strings
//Word info
//Write a program that asks you for a word then shows its length, lowercase, and uppercase values.
const word = prompt("word:");
console.log(`length: ${word.length}`);
console.log(`Lowercase: ${word.toLowerCase()}`);
console.log(`Uppercase: ${word.toUpperCase()}`);
// Vowel count
// Improve the previous program so that it also shows the number of vowels inside the word.
const word = prompt("word:");
console.log(`length: ${word.length}`);
console.log(`Lowercase: ${word.toLowerCase()}`);
console.log(`Uppercase: ${word.toUpperCase()}`);
let vowel = 0;
for (let i = 0; i < word.length; i++) {
const lowercaseletter = word[i].toLowerCase();
if (lowercaseletter === "a" || lowercaseletter === "e" || lowercaseletter === "i" || lowercaseletter === "o" || lowercaseletter === "u" || lowercaseletter === "y" ){
vowel++;
}
}
console.log(`number of vowels: ${vowel}`);