Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff4f1c7

Browse files
committed
Add my answers for week 3
1 parent 9a15e40 commit ff4f1c7

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

Week3/made.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
console.log('1. Strings!');
2+
strings();
3+
console.log('2. Arrays!');
4+
arrays();
5+
console.log('3. More Javascript');
6+
moreJavascript();
7+
8+
function strings() {
9+
const myString = 'hello,this,is,a,difficult,to,read,sentence';
10+
11+
console.log(myString);
12+
13+
const readableString = myString.replace(/,/g, ' ');
14+
15+
console.log(readableString);
16+
17+
const alternateReadableString = myString.split(',').join(' ');
18+
19+
console.log(readableString);
20+
}
21+
22+
function arrays() {
23+
const favouriteAnimals = [ 'blowfish', 'capricorn', 'giraffe' ];
24+
25+
favouriteAnimals.push('turtle');
26+
27+
console.log(favouriteAnimals);
28+
29+
favouriteAnimals.splice(1, 0, 'meerkat');
30+
31+
console.log([ 'blowfish', 'meerkat', 'capricorn', 'giraffe', 'turtle' ]);
32+
console.log(favouriteAnimals);
33+
console.log('The array has a length of:', favouriteAnimals.length);
34+
35+
favouriteAnimals.splice(3, 1);
36+
37+
console.log(favouriteAnimals);
38+
39+
const meerkatIndex = favouriteAnimals.indexOf('meerkat');
40+
41+
console.log('The item you are looking for is at index:', meerkatIndex);
42+
}
43+
44+
function moreJavascript() {
45+
function sum3 (first, second, third) {
46+
return first + second + third;
47+
}
48+
49+
function colorCar(colour) {
50+
return `A ${colour} car`;
51+
}
52+
53+
const anObject = {
54+
wow: 'such object',
55+
};
56+
57+
function printNamesAndValues(someObject) {
58+
console.log(Object.entries(someObject));
59+
}
60+
61+
function vehicleType(color, code) {
62+
const vehicle = (code === 1) ? 'car' : 'motorbike';
63+
64+
console.log(`A ${color} ${vehicle}`);
65+
}
66+
67+
console.log(3 === 3);
68+
69+
const vehicles = [ 'motorbike', 'car', 'caravan', 'bike', 'skateboard' ];
70+
71+
function vehicle(color, code, age) {
72+
const vehicle = vehicles[code - 1];
73+
const state = (age <= 1) ? 'new' : 'used';
74+
75+
console.log(`A ${color} ${state} ${vehicle}`);
76+
}
77+
78+
vehicle('green', 3, 1);
79+
80+
let advertisement = `Amazin Joe's Garage, we service ${vehicles[0]}s`;
81+
82+
for (let i = 1; i < vehicles.length; i++) {
83+
if (i === (vehicles.length - 1)) {
84+
advertisement += ` and ${vehicles[i]}s.`;
85+
continue;
86+
}
87+
88+
advertisement += `, ${vehicles[i]}s`;
89+
}
90+
91+
console.log(advertisement);
92+
93+
const emptyObject = {};
94+
const previousTeachers = {
95+
teachers: [ 'Jim' ],
96+
languages: [ 'Javascript' ],
97+
};
98+
99+
const x = [ 1, 2, 3 ];
100+
const y = [ 1, 2, 3 ];
101+
const z = y;
102+
103+
console.log('Will be false:', x == y);
104+
console.log('Will be false:', x === y);
105+
console.log('Will be false:', x == z);
106+
console.log('Will be true:', y == z);
107+
108+
function same(a,b){
109+
return a.length === b.length
110+
&& a.reduce(
111+
(equalSoFar, newElement, index) => (equalSoFar && newElement === b[index]),
112+
true
113+
);
114+
}
115+
116+
console.log('Will return `string` (which is the type of a type name):', typeof typeof 42);
117+
}

0 commit comments

Comments
 (0)