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

Skip to content

Commit 7295293

Browse files
committed
js1-week3 Homework - Rabie
1 parent b25da4b commit 7295293

17 files changed

+321
-0
lines changed

Week3/Homework-Week3/assets/arrays.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
let favoriteAnimals = ['blowfish', 'capricorn', 'giraffe'];
3+
console.log(favoriteAnimals);
4+
5+
// Add Mauro's favorite animal 'turtle' to the array:
6+
7+
favoriteAnimals.push('turtle');
8+
console.log(favoriteAnimals); // ["blowfish", "capricorn", "giraffe", "turtle"]
9+
10+
// Add Jim's favorite animal as index 1 to the array.
11+
//This can be done using .splice(index, 0, 'replacer')
12+
//0 means keeping of the existing element in the array. While 1 means to remove it
13+
14+
favoriteAnimals.splice(1, 0, 'meerkat');
15+
console.log(favoriteAnimals); // ["blowfish", "meerkat", "capricorn", "giraffe", "turtle"]
16+
17+
console.log('The array has a length of: ' + favoriteAnimals.length);
18+
19+
// Delete en element from the array.
20+
favoriteAnimals.splice(3, 1);
21+
console.log(favoriteAnimals);
22+
23+
// To find the endex of an element in the array:
24+
console.log('The item you are looking for is at index: ' + favoriteAnimals.indexOf('meerkat'));
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
/* Create a function that takes 3 arguments
3+
and returns the sum of the these arguments. */
4+
5+
function sum(a, b, c) {
6+
return a + b + c;
7+
}
8+
console.log(sum(10, 3, 4));
9+
10+
/* Create a function that takes unlimited arguments
11+
and returns the sum of the these arguments. */
12+
13+
function anotherSum() {
14+
let d = 0;
15+
for (let i = 0; i < arguments.length; i++) {
16+
d += arguments[i];
17+
}
18+
return d;
19+
}
20+
console.log(anotherSum(10, 3, 4, 10, -4));
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
/*Use the list of vehicles to write an advertisement.
3+
So that it prints something like: "Amazing Joe's Garage,
4+
we service cars, motorbikes, caravans and bikes.".
5+
(Hint: use a for loop.)*/
6+
const vehicles = [' motorbike', ' scooter', ' caravan', ' bike'];
7+
8+
function advert() {
9+
let advString = '';
10+
11+
for (let i = 0; i < vehicles.length; i++) {
12+
if (i === vehicles.length - 1) {
13+
advString += vehicles[i] + 's.';
14+
} else {
15+
advString += vehicles[i] + 's,';
16+
}
17+
}
18+
return "Amazing Joe's Garage, we service for" + advString;
19+
}
20+
console.log(advert());
21+
//
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
/* What if you add one more vehicle to the list,
3+
can you have that added to the advertisement without
4+
changing the code for question 10?*/
5+
6+
// Yes, this function allows to add more elements to the list
7+
//and will console.log accordingly:
8+
9+
const vehicles = [' motorbike', ' scooter', ' caravan', ' bike'];
10+
11+
function advert() {
12+
let advString = '';
13+
14+
for (let i = 0; i < vehicles.length; i++) {
15+
if (i === vehicles.length - 1) {
16+
advString += vehicles[i] + 's.';
17+
} else {
18+
advString += vehicles[i] + 's,';
19+
}
20+
}
21+
return "Amazing Joe's Garage, we service for" + advString;
22+
}
23+
vehicles.push(' lorry'); // To add new element to the end of the array.
24+
console.log(advert());
25+
//
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// 12: Create an empty object.
3+
const hyf = new Object();
4+
5+
// Create an object that contains the teachers that you have had
6+
//so far for the different modules.
7+
hyf.teachers = 'Philipp, Rob, Unmesh, Bonan, Yash';
8+
9+
// Add a property to the object you just created that contains
10+
//the languages that they have taught you.
11+
hyf.lang = 'html, css, js, cli';
12+
13+
console.log(hyf); //wil log : {teachers: "Philipp, Rob, Unmesh, Bonan, Yash", lang: "html, css, js, cli"}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
let x = [1, 2, 3];
3+
let y = [1, 2, 3];
4+
let z = y;
5+
6+
console.log(x == y); // log false ** MDN "Two distinct objects are never equal for either strict or abstract comparisons."
7+
console.log(x === y); // log false
8+
console.log(z == y); // log true ** MDN "An expression comparing Objects is only true if the operands reference the same Object."
9+
console.log(z == x); // log false
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
/* Show that changing o2 changes o3 (or not)
3+
and changing o1 changes o3(or not).
4+
5+
Does the order that you assign (o3 = o2 or o2 = o3) matter? */
6+
let o1 = { foo: 'bar' };
7+
let o2 = { foo: 'bar' };
8+
let o3 = o2;
9+
10+
// changing o2 changes o3 (or not):
11+
// apply change to o2:
12+
o2.foo = 'bar1, bar2';
13+
console.log(o2); // will log: {foo: "bar1, bar2"}
14+
// check equality:
15+
console.log(o3 === o2); // will log: ture (Coz the operands reference the same Object)
16+
17+
//
18+
// changing o1 changes o3 (or not):
19+
// apply change to o1:
20+
o1.foo = 'barOne, barTwe';
21+
console.log(o1); // will log: {foo: "barOne, barTwe"}
22+
console.log(o1 === o3); // will log: false. (Two distinct objects are never equal for either strict or abstract comparisons.)
23+
24+
/* Does the order that you assign (o3 = o2 or o2 = o3) matter?
25+
** Yes it matters becouze we can't access 'o3' before initialization of o2. */
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
// What does the following code return? (And why?)
3+
4+
let bar = 42;
5+
typeof typeof bar;
6+
7+
console.log(typeof typeof bar); //will log: string
8+
// Typeof returns a string representation of the typeof variable name: bar.
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
/* eate a function named colorCar that receives
3+
a color, and prints out, 'a red car' for example. */
4+
5+
function colorCar(color) {
6+
return 'The car is: ' + color;
7+
}
8+
console.log(colorCar('Red'));
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
/* Create an object and a function that takes the object
3+
as a parameter and prints out all of its properties and values.
4+
*/
5+
6+
const car = { made: 'Honada', year: 2019, color: 'Red', availableNow: true };
7+
8+
function myFunc() {
9+
return car;
10+
}
11+
console.log(myFunc());
12+
13+
//
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Create a function named vehicleType that receives a color,
2+
and a code, 1 for car, 2 for motorbike. And prints 'a blue motorbike'
3+
for example when called as vehicleType("blue", 2)
4+
*/
5+
6+
{
7+
function vehicleType(color, code) {
8+
// declare empty variable
9+
let selectVehicleType;
10+
// use if statement to update the empty variable (selectVehicleType)
11+
if (code === 1) {
12+
selectVehicleType = 'car';
13+
} else if (code === 2) {
14+
selectVehicleType = 'motorbike';
15+
} else {
16+
return 'Error, the values should be only 1 or 2';
17+
}
18+
19+
return 'A ' + color + ' ' + selectVehicleType;
20+
}
21+
console.log(vehicleType('blue', 2));
22+
console.log(vehicleType('White', 1));
23+
console.log(vehicleType('Green', 3));
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
/*
3+
Can you write the following without the if statement,
4+
but with just as a single line with console.log(...);?
5+
6+
7+
if (3 === 3) {
8+
console.log('yes');
9+
} else {
10+
console.log('no');
11+
}
12+
*/
13+
14+
console.log("No, I couldn't find a way to replace if statement with single line of console log");
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
/*Create a function called vehicle, like before,
3+
but takes another parameter called age, so that
4+
vehicle("blue", 1, 5) prints 'a blue used car' */
5+
6+
function vehicle(color, code, age) {
7+
// declare empty variables
8+
var selectVehicle;
9+
let carAge;
10+
11+
// use if statement to update the empty variables:
12+
13+
if (code === 1) {
14+
if (age >= 5) {
15+
selectVehicle = 'car';
16+
carAge = 'used';
17+
return 'A ' + color + ' ' + carAge + ' ' + selectVehicle;
18+
}
19+
} else if (code === 2) {
20+
if (age <= 5) {
21+
selectVehicle = 'motorbike';
22+
carAge = 'new';
23+
return 'A ' + color + ' ' + carAge + ' ' + selectVehicle;
24+
}
25+
} else {
26+
return 'Use valed values';
27+
}
28+
}
29+
console.log(vehicle('blue', 1, 5));
30+
console.log(vehicle('white', 2, 4));
31+
console.log(vehicle('white', 3, 6));
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
/* Make a list of vehicles, you can add "motorbike", "caravan", "bike", or more.
3+
How do you get the third element from that list? */
4+
5+
const vehicles = ['motorbike', 'scooter', 'caravan', 'bike'];
6+
// I get an element by its index number:
7+
console.log(vehicles[2]);
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function vehicle(color, code, age) {
2+
let vehicles = ['motorbike', 'scooter', 'caravan', 'bike'];
3+
let selectVehicle;
4+
let carAge;
5+
6+
for (let i = 0; i < vehicles.length; i++) {
7+
if (code === i) {
8+
selectVehicle = vehicles[i];
9+
}
10+
}
11+
if (age >= 1) {
12+
carAge = 'used';
13+
} else if (age <= 1) {
14+
carAge = 'new';
15+
} else {
16+
return 'Use valed values';
17+
}
18+
return 'A ' + color + ' ' + carAge + ' ' + selectVehicle;
19+
}
20+
21+
console.log(vehicle('Green', 3, 1));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let myString = 'hello,this,is,a,difficult,to,read,sentence';
2+
console.log(myString);
3+
4+
/* Global replace can only be done with a regular expression.
5+
The regular expression in the following includes the global
6+
and ignore case flags which permits replace() to replace each
7+
occurrence of ',' in the string with ' '.
8+
*/
9+
let newstring = myString.replace(/,/gi, ' ');
10+
console.log(newstring);

Week3/Homework-Week3/index.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>JS1 - Week3</title>
9+
</head>
10+
11+
<body>
12+
<h1>Javascript 1</h1>
13+
<h2>Week 3 Homework</h2>
14+
15+
<script src="assets/strings.js"></script>
16+
<script src="assets/arrays.js"></script>
17+
<script src="assets/morejs_1.js"></script>
18+
<script src="assets/morejs_2.js"></script>
19+
<script src="assets/morejs_3.js"></script>
20+
<script src="assets/morejs_4.js"></script>
21+
<script src="assets/morejs_5.js"></script>
22+
<script src="assets/morejs_6.js"></script>
23+
<script src="assets/morejs_7_8.js"></script>
24+
<script src="assets/morejs_9.js"></script>
25+
<script src="assets/morejs_10.js"></script>
26+
<script src="assets/morejs_11.js"></script>
27+
<script src="assets/morejs_12_13_14.js"></script>
28+
<script src="assets/morejs_15.js"></script>
29+
<script src="assets/morejs_16.js"></script>
30+
<script src="assets/morejs_17.js"></script>
31+
32+
33+
</body>
34+
35+
</html>

0 commit comments

Comments
 (0)