diff --git a/omar_qaterge.js b/omar_qaterge.js new file mode 100644 index 000000000..206b5948f --- /dev/null +++ b/omar_qaterge.js @@ -0,0 +1,202 @@ +//1 +console.log('Hello, World!'); +console.log('Ciao, mondo!'); +console.log('Salut, monde!'); + + + + +//2 +//problem: +//console.log('I'm awesome'); + +//Solution: +console.log("I'm awesome"); + + + + +// 3 +let x; +console.log("the value of my variable x will be: undefined"); +console.log(x); +x = 69; +console.log("the value of x will be: 69"); +console.log(x); + + + + +//4 +let y = "JUST DO IT!"; +console.log("the value of my string will be: " + y); +console.log(y); +y = "maybe later..."; +console.log("the value of my string will be: " + y); +console.log(y); + + + + +//5 +//method 1 +let z = 7.25; +console.log(z); +const a = Math.round(z); +console.log(a); +const maxvalue = Math.max(a, z); +console.log("the highest value of the two is: " + maxvalue); + + + + +//method 2 +let z = 7.25; +console.log(z); +const a = Math.round(z); +console.log(a); +let highest_value; +if (z > a) { + highest_value = z + console.log("the highest value of the two is: " + highest_value); +} else { + highest_value = a; + console.log("the highest value of the two is: " + highest_value); +} + + + + + +//mothod 3 +let z = 7.25; +console.log(z); +const a = Math.round(z); +console.log(a); +const s = z - a; +let highest_value; +if (s > a - z) { + highest_value = a + s; + console.log("the highest value of the two is: " + highest_value); +} else { + highest_value = z + -s; + console.log("the highest value of the two is: " + highest_value); +} + + + + +//method 4 +let z = 7.25; +console.log(z); +let a = z; +if (a - Math.trunc(a) <= 0.4) { + a = Math.trunc(a); +} else { + a = 1 - (a - Math.trunc(a)) + a; +} +console.log(a); +let highest_value; +if (z > a) { + highest_value = z; + console.log("the highest value of the two is: " + highest_value); +} else { + highest_value = a; + console.log("the highest value of the two is: " + highest_value); +} + + + + +//method 5 +let z = 7.25; +console.log(z); +let a = z; +n = (a + "").split("."); +i =(n.splice(-1, 1)); +let decimal = i[0]; +if (decimal.charAt(0) <= 4) { + a = Math.trunc(a); +} else { + a = 1 - (a - Math.trunc(a)) + a; +} +console.log(a); +let highest_value; +if (z > a) { + highest_value = z; + console.log("the highest value of the two is: " + highest_value); +} else { + highest_value = a; + console.log("the highest value of the two is: " + highest_value); +} + + + + +//6 +const class_names = []; +console.log("the value of the array is null which is nothing because it's empty"); +console.log(class_names); +const domesticated_animals = ['dog', 'cat', 'horse', 'yak', 'alpaca']; +console.log(domesticated_animals); +domesticated_animals.push('baby pig'); +console.log(domesticated_animals); + + + + +//7 +//more strings +let myString = "this is a test"; +console.log(myString); +console.log('the length of my string is: '+ myString.length); + + + + +//8 +const string = 'hello, world!'; +const number = 5; +const boolean = true; +const object = {firstName:"Elon", lastName:"Musk"}; +console.log(string); +console.log(number); +console.log(boolean); +console.log(object); +console.log('the type of hello world is string'); +console.log(typeof string); +console.log('the type of 5 is number'); +console.log(typeof number); +console.log('the type of true is boolean'); +console.log(typeof boolean); +console.log('the type of firstName:"Elon", lastName:"Musk" is object'); +console.log(typeof object); + +if (typeof boolean == typeof object) { + console.log('SAME TYPE'); +} else { + console.log('NOT THE SAME TYPE'); +} + + + + +//9 +let x = 7; +console.log("the value of x will be 1 because it's the remainder"); +console.log(x = x % 3); +console.log('% is the modulus operator, it will let you have the remainder of the division'); +console.log('The % symbol is used in most programming languages, including JavaScript, as Modulu'); +console.log('modulo is the operation use to find the remainder after division of one number by another'); + + + + +//10 +console.log('Can you store multiple types in an array? Numbers and strings?'); +console.log('ANSWER: an array can store mutiple types of variables, Example:'); +console.log (array = ['Hello!!!!', 454, true]); +console.log("Can you compare infinities? (Not in Eyad's world) - does 6/0 === 10/0? How can you test this?"); +console.log('ANSWER: 6/0 === 10/0 is true because both sides have the same value (both infinite) and type,'); +console.log('we can test it by running 6/0 === 10/0 straight in the console or like this:'); +console.log(6/0 === 10/0); \ No newline at end of file diff --git a/week3.js b/week3.js new file mode 100644 index 000000000..810e47636 --- /dev/null +++ b/week3.js @@ -0,0 +1,223 @@ +// String and Array challenges + +// 1 strings: + +let myString = "hello,this,is,a,difficult,to,read,sentence"; +console.log(myString); +console.log(myString.length); +myString = myString.replace(/,/g, ' '); // replace commas with spaces +console.log(myString); + + + +// 2 Arrays: +let favoriteAnimals = ["blowfish", "capricorn", "giraffe"]; +favoriteAnimals.push("turtle"); // adding Mauro's favorite animal at the end of the previous array +console.log(favoriteAnimals); +favoriteAnimals.splice(1, 0, "meerkat"); +console.log("using splice() a method that changes the contents of an array by removing or replacing existing elements and/or adding new elements)\n \ +in .splice(1, 0, 'meerkat') the 1 defines the index that i want to work with and 0 defines the number of elements that i want to replace.\n \ +so the value of the array will be:\n \ +[\"blowfish\", \"meerkat\", \"capricorn\", \"giraffe\", \"turtle\"]"); +console.log(favoriteAnimals); +console.log("The array has a length of: " + favoriteAnimals.length); +favoriteAnimals.splice(3,1); // delete giraffe from the previous array +console.log(favoriteAnimals); +console.log("The item you are looking for is at index: " + favoriteAnimals.findIndex(animal => animal === "meerkat")); + + + + +// More JavaScript + +//1 +function sum(x, y, z) { + const result = x + y + z; + return result; +} + + + +//2 +function colorCar(color) { + const result = 'a ' + color + ' car'; + return result; +} + + + +//3 +function info(person = {firstName:"mark", lastName:"trump", age:50, eyeColor:"blue"}) { + return person; +} + + + +//4 +function vehicleType(color, code){ + if(code === 1){ + return console.log("a " + color + " car")} + else if(code === 2){ + return console.log("a " + color + " motorbike")} +} + + + +//5 +3 === 3 ? console.log("yes") : console.log("no"); + + + +//6 +function vehicle(color, code, age){ + if(code === 1){ + if(age > 0){ + console.log("a " + color + " used car"); + } + else{console.log("a " + color + " car");} + } + else if(code === 2){ + if(age > 0){ + console.log("a " + color + " used motorbike"); + } + else{ + console.log("a " + color + " motorbike");} + } +} + + + +//7 +// method 1: using objects (adding another layer of objects for fun!) +vehicle = { + "motor": { + id: "motorbike", + id2: "bus", + id3: "truck", + id4: "car" + }, + "railed": { + id: "train", + id2: "tram", + }, + "aircraft": { + id: "airplane", + id2: "helicopter", + } +} +const firstKey = Object.keys(vehicle)[0]; +const secondKey = Object.keys(vehicle)[1]; +const thirdKey = Object.keys(vehicle)[2]; +//---------------------------------------------------- +console.log(vehicle["aircraft"]["id2"]); +//logs helicopter +console.log(vehicle[thirdKey]["id2"]); +//logs helicopter +//---------------------------------------------------- + +//method 2: using arrays +const vehicles = ["motorbike", "caravan", "bike", "bus"]; + + + + +//8 +console.log(vehicles[2]); + + + + +//9 +const vehicles = ["motorbike", "caravan", "bike", "bus"]; + + +function vehicle(color,code,age){ + let i; + if(code === 1){ + i = vehicles[0]; + } + else if(code === 2){ + i = vehicles[1]; + } + else if(code === 3){ + i = vehicles[2] + } + else if(code === 4){ + i = vehicles[3] + } + + let x; + if(age <= 1){ + x = "new"; + } + else{ + x = "used"; + } + console.log("a",color,x,i); +} +vehicle("green", 3, 1) + +///////////////////////////////////////////////////////// +//10 +const vehicles = ["car", "motorbike", "caravan", "bike"]; + +let vehicleToService=""; +for(let counter = 0; counter < vehicles.length; counter ++){ + if(counter === vehicles.length - 1){ + vehicleToService += vehicles[counter] + "s."; + } + else {vehicleToService += vehicles[counter] + "s, ";} + } + + console.log(" \"Amazing Joe's Garage, we service" , vehicleToService,"\""); + + + +//11 +const vehicles = ["car", "motorbike", "caravan", "bike"]; +vehicles.push("plane"); +console.log(vehicles); +let vehicleToService=""; +for(let counter = 0; counter < vehicles.length; counter ++){ + if(counter === vehicles.length - 1){ + vehicleToService += vehicles[counter] + "s."; + } + else {vehicleToService += vehicles[counter] + "s, ";} + } + +console.log(" \"Amazing Joe's Garage, we service" , vehicleToService,"\""); + + + +//12 +const object = {}; + + + +//13 +const person = {htmlCss:"Philipp Beau", cli:"Unmesh Joshi", JavaScript:"Yash Kapila"}; + + + +//16 +let o1 = { foo: "bar" }; +let o2 = { foo: "bar" }; +let o3 = o2; + +console.log(o3); +o2.foo = "something"; +console.log(o3); +o1.foo = "and something else"; +console.log(o3); + +//Does the order that you assign (o3 = o2 or o2 = o3) matter? +//yes, because the right side is being assigned to the left side, otherwise it will show that the variable is not defined. + + + +//What does the following code return? (And why?) +let bar = 42; +typeof typeof bar; + +//typeof bar; returns "number" because a number is assigned to bar as a value +//typeof typeof bar; returns "string" because it is returning the type of "number" which is a string \ No newline at end of file