diff --git a/Week1/homework/background.jpeg b/Week1/homework/background.jpeg new file mode 100644 index 000000000..75a0dd9c5 Binary files /dev/null and b/Week1/homework/background.jpeg differ diff --git a/Week1/homework/img/5th_horseman.jpg b/Week1/homework/img/5th_horseman.jpg new file mode 100644 index 000000000..fc44bfccc Binary files /dev/null and b/Week1/homework/img/5th_horseman.jpg differ diff --git a/Week1/homework/img/da_vinci_code.jpg b/Week1/homework/img/da_vinci_code.jpg new file mode 100644 index 000000000..563a49203 Binary files /dev/null and b/Week1/homework/img/da_vinci_code.jpg differ diff --git a/Week1/homework/img/duc_man.jpg b/Week1/homework/img/duc_man.jpg new file mode 100644 index 000000000..0bf990b8e Binary files /dev/null and b/Week1/homework/img/duc_man.jpg differ diff --git a/Week1/homework/img/hunger games 1.jpg b/Week1/homework/img/hunger games 1.jpg new file mode 100644 index 000000000..7c7649857 Binary files /dev/null and b/Week1/homework/img/hunger games 1.jpg differ diff --git a/Week1/homework/img/hunger_games_mockingjay.jpg b/Week1/homework/img/hunger_games_mockingjay.jpg new file mode 100644 index 000000000..69cb98679 Binary files /dev/null and b/Week1/homework/img/hunger_games_mockingjay.jpg differ diff --git a/Week1/homework/img/improbable.jpg b/Week1/homework/img/improbable.jpg new file mode 100644 index 000000000..ed5fbe16d Binary files /dev/null and b/Week1/homework/img/improbable.jpg differ diff --git a/Week1/homework/img/miserables.jpg b/Week1/homework/img/miserables.jpg new file mode 100644 index 000000000..44a2c740c Binary files /dev/null and b/Week1/homework/img/miserables.jpg differ diff --git a/Week1/homework/img/parbat.jpg b/Week1/homework/img/parbat.jpg new file mode 100644 index 000000000..a7e3835ad Binary files /dev/null and b/Week1/homework/img/parbat.jpg differ diff --git a/Week1/homework/img/peetvader.jpg b/Week1/homework/img/peetvader.jpg new file mode 100644 index 000000000..53c21a9a1 Binary files /dev/null and b/Week1/homework/img/peetvader.jpg differ diff --git a/Week1/homework/img/under_ocean.jpg b/Week1/homework/img/under_ocean.jpg new file mode 100644 index 000000000..717b99863 Binary files /dev/null and b/Week1/homework/img/under_ocean.jpg differ diff --git a/Week2/class exercise array/index.html b/Week2/class exercise array/index.html new file mode 100644 index 000000000..0d71ecc04 --- /dev/null +++ b/Week2/class exercise array/index.html @@ -0,0 +1,13 @@ + + + + + + + Document + + + + + + diff --git a/Week2/class exercise array/index.js b/Week2/class exercise array/index.js new file mode 100644 index 000000000..c6142efca --- /dev/null +++ b/Week2/class exercise array/index.js @@ -0,0 +1,93 @@ +//JS function to return first element, and pass a number as parameter +const arrayMine = [ + 'paramater_is_study', + 1, + 'time', + 'great op', + 'good work', + 1, + 2, + 2341, + 'parameter', + 'time', +]; +console.log(arrayMine); + +var n = window.prompt('Please enter first how many numbers you want to log'); +var num1 = parseInt(n); + +function nthItem(array, num1) { + var arrayX = []; + for (let i = 0; i < num1; i++) { + arrayX.push(array[i]); + } + return arrayX; +} +console.log(nthItem(arrayMine, n)); + +//a function which accepts a number as input and insert dashes btw each two even number: +function evenNumbers() { + var number = window.prompt('Please enter a number'); + const string = number.toString(); + const result = [string[0]]; + for (let i = 0; i < string.length; i++) { + if (string[i - 1] % 2 === 0 && string[i] % 2 === 0) { + result.push('-', string[i]); + } else if (string[i - 1] % 2 === 1 && string[i] % 2 === 0) { + result.push(string[i]); + } else if (string[i - 1] % 2 === 0 && string[i] % 2 === 1) { + result.push('-', string[i]); + } else if (string[i - 1] % 2 === 1 && string[i] % 2 === 1) { + result.push(string[i]); + } + } + console.log(result.join('')); +} +evenNumbers(); + +//finding the most frequent item of an array +function findMostFrequent(array) { + var map = {}; + var mostFrequentElement = array[0]; + for (var i = 0; i < array.length; i++) { + if (!map[array[i]]) { + map[array[i]] = 1; + } else { + ++map[array[i]]; + if (map[array[i]] > map[mostFrequentElement]) { + mostFrequentElement = array[i]; + console.log('The most frequent element of the array is: ' + mostFrequentElement); + } else if (map[array[i]] === map[mostFrequentElement]) { + console.log('The array has more than one most frequent elements'); + console.log( + 'The most frequent elements of the array are: ' + mostFrequentElement + ', ' + array[i], + ); + } + } + } +} + +findMostFrequent(arrayMine); + +//function accepting a string as input and swapping the case of each character +function swapCase() { + var str = window.prompt('Please enter something'); + + //first we define the alphabet and check the letters according to that + var upperCases = 'ABCÇDEFGHIİJKLMNOÖPQRSTUÜVWXYZ'; + var lowerCases = 'abcçdefghıijklmnoöpqrstuüvwxyz'; + + //we will check each letter according to upper and lower case and transform it: + var result = []; + for (var x = 0; x < str.length; x++) { + if (upperCases.indexOf(str[x]) !== -1) { + result.push(str[x].toLowerCase()); + } else if (lowerCases.indexOf(str[x]) !== -1) { + result.push(str[x].toUpperCase()); + } else { + result.push(str[x]); + } + } + console.log(result.join('')); +} +swapCase(); diff --git a/Week2/class exercise json/index.html b/Week2/class exercise json/index.html new file mode 100644 index 000000000..08fbd5ac4 --- /dev/null +++ b/Week2/class exercise json/index.html @@ -0,0 +1,15 @@ + + + + + + + Document + + + +
+ + + + diff --git a/Week2/class exercise json/index.js b/Week2/class exercise json/index.js new file mode 100644 index 000000000..e9a24dd4c --- /dev/null +++ b/Week2/class exercise json/index.js @@ -0,0 +1,76 @@ +//JS program for getting the length of an object +var myObj = {}; +myObj['full'] = 'Galatasaray'; +myObj['short'] = 'GS'; +myObj['league'] = 'Super League'; +myObj['position'] = 1; +Object.size = function(obj) { + var size = 0, + key; + for (key in obj) { + if (obj.hasOwnProperty(key)) size++; + } + return size; +}; + +var size = Object.size(myObj); +console.log('The length of the object is: ' + size); + +//js function to check if object contains given prop +var x = window.prompt('Please enter a property to check if it exist in the object'); +Object.check = function(obj, x) { + for (let key in obj) { + if (x === obj.hasOwnProperty(key)) console.log('This is a property of object'); + else console.log("This property doesn't exist in this object"); + } +}; +Object.check(myObj); + +//js program to create clock, console the seconds to html +function countTime() { + var today = new Date(); + var h = today.getHours(); + var m = today.getMinutes(); + var s = today.getSeconds(); + m = checkTime(m); + s = checkTime(s); + document.getElementById('txt').innerHTML = h + ':' + m + ':' + s; + var t = setTimeout(countTime, 500); +} +function checkTime(i) { + if (i < 10) { + i = '0' + i; + } // add zero in front of numbers < 10 + return i; +} + +//js program to create clock, console the seconds +function my_Clock() { + this.cur_date = new Date(); + this.hours = this.cur_date.getHours(); + this.minutes = this.cur_date.getMinutes(); + this.seconds = this.cur_date.getSeconds(); +} +my_Clock.prototype.run = function() { + setInterval(this.update.bind(this), 1000); +}; +my_Clock.prototype.update = function() { + this.updateTime(1); + console.log(this.hours + ':' + this.minutes + ':' + this.seconds); +}; +my_Clock.prototype.updateTime = function(secs) { + this.seconds += secs; + if (this.seconds >= 60) { + this.minutes++; + this.seconds = 0; + } + if (this.minutes >= 60) { + this.hours++; + this.minutes = 0; + } + if (this.hours >= 24) { + this.hours = 0; + } +}; +var clock = new my_Clock(); +clock.run(); diff --git a/Week2/homework/index.html b/Week2/homework/index.html new file mode 100644 index 000000000..ce6448a5a --- /dev/null +++ b/Week2/homework/index.html @@ -0,0 +1,14 @@ + + + + + + + Document + + + + + + + diff --git a/Week3/homework/exercise/index.html b/Week3/homework/exercise/index.html new file mode 100644 index 000000000..26eb5ecc1 --- /dev/null +++ b/Week3/homework/exercise/index.html @@ -0,0 +1,15 @@ + + + + + + + week3 doc + + + +
+ + + + diff --git a/Week3/homework/exercise/index.js b/Week3/homework/exercise/index.js new file mode 100644 index 000000000..d18b3c682 --- /dev/null +++ b/Week3/homework/exercise/index.js @@ -0,0 +1,51 @@ +/* to do: +- Create a class called `Form` +- Add a `null` variable (eg: formElement) +- Create a methods `createButton` and create an HTML button element in it +*/ + +// create class call 'Form' +class Form { + constructor() { + //give a form variable with 'null' as a value + this.formElement = null; + + console.log('hello'); + //create a form element and put in formElement + this.formElement = document.createElement('form'); + document.getElementsByTagName('div')[0].appendChild(this.formElement); + } + //add method to this class to create a button + createButton() { + //create button + this.newButton = document.createElement('button'); + this.newButton.type = 'submit'; + + //and add to formElement + this.formElement.appendChild(this.newButton); + this.newButton.innerHTML = 'Click this button'; + } + render() {} + createInput() { + //create input + this.input = document.createElement('input'); + this.input.type = 'text'; + //and add to formElement + this.formElement.appendChild(this.input); + } + createCheckbox() { + //create checkbox element + this.checkbox = document.createElement('input'); + this.checkbox.type = 'checkbox'; + //and add to formElement + this.formElement.appendChild(this.checkbox); + } +} + +const form1 = new Form('formId'); +form1.createButton(); +form1.createInput(); +form1.createCheckbox(); +console.log(form1); + +//
diff --git a/Week3/homework/index.html b/Week3/homework/index.html new file mode 100644 index 000000000..17f09494f --- /dev/null +++ b/Week3/homework/index.html @@ -0,0 +1,13 @@ + + + + + + + week3 doc + + + + + + diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..0019e2478 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -1,8 +1,7 @@ 'use strict'; function foo(func) { - // What to do here? - // Replace this comment and the next line with your code + func(); console.log(func); } diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..1304c364d 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -2,19 +2,36 @@ function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { const numbers = []; + //Generate an array containing values from start value to end value (inclusive): + while (startIndex <= stopIndex) { + numbers.push(startIndex++); + } - // Replace this comment and the next line with your code - console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers); + //Take the newly created array and iterate over it, + //and calling the first callback if the array value is divisible by 3: + //The function should call the second callback if the array value is divisible by 5: + + numbers.filter(function(number) { + if (number % 3 === 0 && number % 5 === 0) { + sayThree(number); + console.log('...and...'); + sayFive(number); + } else if (number % 3 === 0) { + sayThree(number); + } else if (number % 5 === 0) { + sayFive(number); + } + }); + + console.log('Our array is ' + numbers); } function sayThree(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(number + ' is divisible by 3'); } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(number + ' is divisible by 5'); } threeFive(10, 15, sayThree, sayFive); diff --git a/Week3/homework/step2-3.1.js b/Week3/homework/step2-3.1.js new file mode 100644 index 000000000..e33dcb3ba --- /dev/null +++ b/Week3/homework/step2-3.1.js @@ -0,0 +1,22 @@ +'use strict'; + +// Use a 'for' loop +function repeatStringNumTimesWithFor(str, num) { + // eslint-disable-next-line prefer-const + let result = ''; + + for (let i = 1; i <= num; i++) { + result += str; + } + + return result; +} + +console.log('for', repeatStringNumTimesWithFor('abc', 3)); + +// Do not change or remove anything below this line +module.exports = { + repeatStringNumTimesWithFor, + repeatStringNumTimesWithWhile, + repeatStringNumTimesWithDoWhile, +}; diff --git a/Week3/homework/step2-3.2.js b/Week3/homework/step2-3.2.js new file mode 100644 index 000000000..450b76cba --- /dev/null +++ b/Week3/homework/step2-3.2.js @@ -0,0 +1,23 @@ +'use strict'; + +// Use a 'while' loop +function repeatStringNumTimesWithWhile(str, num) { + // eslint-disable-next-line prefer-const + let result = ''; + + while (num > 0) { + result += str; + num--; + } + + return result; +} + +console.log('while', repeatStringNumTimesWithWhile('abc', 3)); + +// Do not change or remove anything below this line +module.exports = { + repeatStringNumTimesWithFor, + repeatStringNumTimesWithWhile, + repeatStringNumTimesWithDoWhile, +}; diff --git a/Week3/homework/step2-3.3.js b/Week3/homework/step2-3.3.js new file mode 100644 index 000000000..1b4e6dd50 --- /dev/null +++ b/Week3/homework/step2-3.3.js @@ -0,0 +1,23 @@ +'use strict'; + +// Use a 'do...while' loop +function repeatStringNumTimesWithDoWhile(str, num) { + // eslint-disable-next-line prefer-const + let result = ''; + + do { + result += str; + num--; + } while (num > 0); + + return result; +} + +console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3)); + +// Do not change or remove anything below this line +module.exports = { + repeatStringNumTimesWithFor, + repeatStringNumTimesWithWhile, + repeatStringNumTimesWithDoWhile, +}; diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..1c4f8c6d9 100644 --- a/Week3/homework/step2-4.js +++ b/Week3/homework/step2-4.js @@ -1,10 +1,13 @@ 'use strict'; function Dog() { - // add your code here + this.name = 'Albert'; + this.color = 'brown'; + this.numLegs = 4; } -const hound = new Dog(); +var hound = new Dog(); +console.log(hound); // Do not change or remove anything below this line module.exports = hound; diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..8c1894638 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -2,16 +2,19 @@ function multiplyAll(arr) { // eslint-disable-next-line - let product = 1; + var product = 1; - // Replace this comment and the next line with your code - console.log(arr, product); + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + product *= arr[i][j]; + } + } return product; } -const result = multiplyAll([[1, 2], [3, 4], [5, 6]]); -console.log(result); // 720 +const result = multiplyAll([[1, 2], [3, 4], [5, 6, 7]]); +console.log(result); // 5040 // Do not change or remove anything below this line module.exports = multiplyAll; diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index ffe95b9f7..3763ea5fd 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -4,18 +4,43 @@ const arr2d = [[1, 2], [3, 4], [5, 6]]; const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; function flattenArray2d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + let flatArr = []; + for (let i in arr) { + for (let j in arr[i]) { + flatArr.push(arr[i][j]); + } + } + return flatArr; } function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + let flatArr = []; + for (let i in arr) { + for (let j in arr[i]) { + for (let k in arr[i][j]) { + flatArr.push(arr[i][j][k]); + } + } + } + return flatArr; } console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6] console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8] +/* +Flatten out all the items of an array with x dimensions: +var newArray = arr.flat([depth]); +*/ + +//Flatten out all the items of an array with 2 dimensions: +var newArray2d = arr2d.flat([2]); +console.log(newArray2d); // -> [1, 2, 3, 4, 5, 6] + +//Flatten out all the items of an array with 3 dimensions: +var newArray3d = arr3d.flat([2]); +console.log(newArray3d); // -> [1, 2, 3, 4, 5, 6, 7, 8] + // Do not change or remove anything below this line module.exports = { flattenArray2d, diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..7cead70ce 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -8,7 +8,7 @@ function f1(val) { f1(x); -console.log(x); +console.log(x); // --> 9 const y = { x: 9 }; function f2(val) { @@ -18,6 +18,15 @@ function f2(val) { f2(y); -console.log(y); +console.log(y); // --> {x: 10} // Add your explanation as a comment here +/* In the first function(f1(val)), the value of the variable is passed an argument to the function, +so that whatever happens inside the function doesn't affect the value of the variable 'x' itself. + + But in the second function(f2(val)), because the variable 'y' itself an object, +while passing y as as an argument to the function, actually we pass the references of the object as +argument to the function, so that once function has access to references of the object keys, then +whatever happens to those references inside the function affects the value of the object variable 'y' +itself. +*/ diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..cf622e2cb 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -3,12 +3,16 @@ const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c']; function makeUnique(arr) { - // Replace this comment and the next line with your code - console.log(arr); + var unique = []; + for (let i in arr) { + if (unique.includes(arr[i])) continue; //if the statement inside if is true, it will continue(meaning it will skip one iteration) + unique.push(arr[i]); + } + return unique; } const uniqueValues = makeUnique(values); -console.log(uniqueValues); +console.log(uniqueValues); // ["a", "b", "c", "d", "e", "f"] // Do not change or remove anything below this line module.exports = makeUnique; diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..9c2a2a30d 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/step3.js @@ -1,8 +1,9 @@ 'use strict'; function createBase(base) { - // Replace this comment and the next line with your code - console.log(base); + return function(x) { + return base + x; + }; } const addSix = createBase(6); @@ -10,5 +11,8 @@ const addSix = createBase(6); console.log(addSix(10)); // returns 16 console.log(addSix(21)); // returns 27 +const addTen = createBase(10); +console.log(addTen(30)); // returns 40 + // Do not change or remove anything below this line module.exports = createBase;