├── 10DaysofJavaScript.js ├── LICENSE └── README.md /10DaysofJavaScript.js: -------------------------------------------------------------------------------- 1 | /************** Day 0: Hello, World! **************/ 2 | 3 | function greeting(parameterVariable) { 4 | // This line prints 'Hello, World!' to the console: 5 | console.log("Hello, World!"); 6 | 7 | // Write a line of code that prints parameterVariable to stdout using console.log: 8 | console.log(parameterVariable); 9 | } 10 | 11 | /************** Day 0: Data Types **************/ 12 | /* Method 1 */ 13 | const firstString = "HackerRank "; 14 | // Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number type) on a new line. 15 | console.log(firstInteger + Number(secondInteger)); 16 | 17 | // Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number type) on a new line. 18 | 19 | console.log(firstDecimal + Number(secondDecimal)); 20 | // Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The variable 'firstString' must be printed first. 21 | console.log(firstString + secondString); 22 | 23 | /* Method 2 */ 24 | const firstString = "HackerRank "; 25 | // Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number type) on a new line. 26 | console.log(firstInteger + parseInt(secondInteger)); 27 | 28 | // Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number type) on a new line. 29 | console.log(firstDecimal + parseFloat(secondDecimal)); // Number'da kullanılabilir. 30 | 31 | // Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The variable 'firstString' must be printed first. 32 | console.log(firstString.concat(secondString)); 33 | 34 | /************** Day 1: Arithmetic Operators **************/ 35 | function getArea(length, width) { 36 | let area; 37 | // Write your code here 38 | area = length * width; 39 | return area; 40 | } 41 | 42 | function getPerimeter(length, width) { 43 | let perimeter; 44 | // Write your code here 45 | perimeter = 2 * (length + width); 46 | return perimeter; 47 | } 48 | 49 | /************** Day 1: Functions **************/ 50 | /* Method 1 */ 51 | 52 | /* 53 | * Create the function factorial here 54 | */ 55 | function factorial(n) { 56 | if (n < 2) { 57 | return 1; 58 | } 59 | return n * factorial(n - 1); 60 | } 61 | 62 | /* Method 2*/ 63 | /* 64 | * Create the function factorial here 65 | */ 66 | function factorial(n) { 67 | if (n === 0) { 68 | return 1; 69 | } else { 70 | return n * factorial(n - 1); 71 | } 72 | } 73 | 74 | /************** Day:1 Let and Const **************/ 75 | 76 | /* Method 1*/ 77 | function main() { 78 | // Write your code here. Read input using 'readLine()' and print output using 'console.log()'. 79 | const PI = 3.141592653589793238462643383; 80 | const r = 2.6; 81 | // Print the area of the circle: 82 | let area = PI * r * r; 83 | console.log(area); 84 | // Print the perimeter of the circle: 85 | let perimeter = 2 * PI * r; 86 | console.log(perimeter); 87 | } 88 | 89 | /* Method 2*/ 90 | function main() { 91 | // Write your code here. Read input using 'readLine()' and print output using 'console.log()'. 92 | const PI = Math.PI; 93 | let r = parseFloat(readLine()); 94 | // Print the area of the circle: 95 | let area = PI * Math.pow(r, 2); 96 | console.log(area); 97 | // Print the perimeter of the circle: 98 | let perimeter = 2 * PI * r; 99 | console.log(perimeter); 100 | } 101 | 102 | /************** Day:2 Conditional Statements: If-Else **************/ 103 | 104 | function getGrade(score) { 105 | let grade; 106 | // Write your code here 107 | if (score >= 25 && score <= 30) { 108 | return (grade = "A"); 109 | } else if (score >= 20 && score <= 25) { 110 | return (grade = "B"); 111 | } else if (score >= 15 && score <= 20) { 112 | return (grade = "C"); 113 | } else if (score >= 10 && score <= 15) { 114 | return (grade = "D"); 115 | } else if (score >= 5 && score <= 10) { 116 | return (grade = "E"); 117 | } else if (score >= 0 && score <= 5) { 118 | return (grade = "F"); 119 | } 120 | return grade; 121 | } 122 | 123 | function main() { 124 | const score = +readLine(); 125 | 126 | console.log(getGrade(score)); 127 | } 128 | 129 | /************** Day 2: Conditional Statements: Switch **************/ 130 | 131 | /* Hint: You can get the letter at some index in using the syntax s[i] or s.charAt(i). */ 132 | 133 | /* Method 1: s.charAt(i)*/ 134 | 135 | function getLetter(s) { 136 | let letter; 137 | // Write your code here 138 | switch (s.charAt(0)) { 139 | case "a": 140 | case "e": 141 | case "i": 142 | case "o": 143 | case "u": 144 | letter = "A"; 145 | break; 146 | 147 | case "b": 148 | case "c": 149 | case "d": 150 | case "f": 151 | case "g": 152 | letter = "B"; 153 | break; 154 | 155 | case "h": 156 | case "j": 157 | case "k": 158 | case "l": 159 | case "m": 160 | letter = "C"; 161 | break; 162 | 163 | case "n": 164 | case "p": 165 | case "q": 166 | case "r": 167 | case "s": 168 | case "t": 169 | case "v": 170 | case "w": 171 | case "x": 172 | case "y": 173 | case "z": 174 | letter = "D"; 175 | break; 176 | } 177 | return letter; 178 | } 179 | 180 | /* Method 2: s[i] */ 181 | 182 | function getLetter(s) { 183 | let letter; 184 | // Write your code here 185 | switch (true) { 186 | case "aeiou".includes(s[0]): 187 | letter = "A"; 188 | break; 189 | case "bcdfg".includes(s[0]): 190 | letter = "B"; 191 | break; 192 | case "hjklm".includes(s[0]): 193 | letter = "C"; 194 | break; 195 | case "npqrstvwxyz".includes(s[0]): 196 | letter = "D"; 197 | break; 198 | } 199 | } 200 | 201 | /************** Day 2: Loops **************/ 202 | 203 | /* Method 1 */ 204 | 205 | var vowels = ["a", "e", "i", "o", "u"]; 206 | function vowelsAndConsonants(s) { 207 | for (var index = 0; index < s.length; index++) { 208 | if (vowels.includes(s[index])) { 209 | console.log(s[index]); 210 | } 211 | } 212 | 213 | for (var index = 0; index < s.length; index++) { 214 | if (!vowels.includes(s[index])) { 215 | console.log(s[index]); 216 | } 217 | } 218 | } 219 | 220 | /* Method 2 */ 221 | var vowels = ["a", "e", "i", "o", "u"]; 222 | function vowelsAndConsonants(s) { 223 | for (var i = 0; i < s.length; i++) { 224 | if (vowels.indexOf(s[i]) > -1) { 225 | console.log(s[i]); 226 | } 227 | } 228 | 229 | for (var j = 0; j < s.length; j++) { 230 | if (vowels.indexOf(s[j]) < 0) { 231 | console.log(s[j]); 232 | } 233 | } 234 | } 235 | 236 | /* Method 3 */ 237 | 238 | function vowelsAndConsonants(s) { 239 | const vowels = "aeiou"; 240 | var consonants = ""; 241 | 242 | for (var i = 0; i < s.length; i++) { 243 | if (vowels.includes(s[i])) { 244 | console.log(s[i]); 245 | } else { 246 | consonants += s[i] + "\n"; 247 | } 248 | } 249 | console.log(consonants.trim()); 250 | } 251 | 252 | /************** Day 3: Arrays **************/ 253 | 254 | /** 255 | * Return the second largest number in the array. 256 | * @param {Number[]} nums - An array of numbers. 257 | * @return {Number} The second largest number in the array. 258 | **/ 259 | 260 | function getSecondLargest(nums) { 261 | // Complete the function 262 | nums.sort(function (x, y) { 263 | return y - x; //buyukten kucuge sıralandı 264 | }); 265 | let results = [...new Set(nums)]; 266 | return results[1]; 267 | } 268 | 269 | /************ Day 3: Try, Catch, and Finally ************/ 270 | 271 | /* Method 1 */ 272 | 273 | function reverseString(s) { 274 | try { 275 | let splitString = s.split(""); //split array'i bölmek için kullanılır 276 | let reverseArray = splitString.reverse(); //reverse alfabetik ters sıralar 277 | let joinArray = reverseArray.join(""); //join string yapar (4321) 278 | console.log(joinArray); 279 | } catch (error) { 280 | //error sembolik burada 281 | //Yeni bir satıra s yazdırın. Herhangi bir özel durum oluşturulmadıysa, bu, ters çevrilmiş dize olmalıdır; bir istisna atıldıysa, bu orijinal dize olmalıdır. 282 | console.log(error.message); 283 | console.log(s); 284 | } 285 | } 286 | 287 | /* Method 2 */ 288 | 289 | function reverseString(s) { 290 | try { 291 | s = s.split("").reverse().join(""); //try ve catch arasına çalışacak kodlar yazılır. 292 | } catch (error) { 293 | //error sembolik burada 294 | console.log("s.split not a function"); //catch ile finally arasına hata yakalandığında çalışacak kodlar yazılır. 295 | } finally { 296 | console.log(s); //finally'den sonra ise try tamamlandıktan sonra hata olsada olmasada çalışacak kodlar yazılır. 297 | } 298 | } 299 | 300 | /* Method 3 */ 301 | 302 | function reverseString(s) { 303 | typeof s !== "string" 304 | ? console.log("s.split is not a function") 305 | : (s = s.split("").reverse().join("")); 306 | console.log(s); 307 | } 308 | 309 | /************** Day:3 Throw **************/ 310 | 311 | function isPositive(a) { 312 | if (a > 0) { 313 | return "YES"; 314 | } else if (a === 0) { 315 | throw Error("Zero Error"); 316 | } else if (a < 0) { 317 | throw Error("Negative Error"); 318 | } 319 | //throw new Error şeklinde de kullanabiliridk. Burada özel bi hata yazdırmak için throw kullandık. 320 | } 321 | 322 | /************** Day:4 Create a Rectangle Object **************/ 323 | 324 | /* Method 1 */ 325 | function Rectangle(a, b) { 326 | return { 327 | length: a, 328 | width: b, 329 | perimeter: 2 * (a + b), 330 | area: a * b, 331 | }; 332 | //object oluşturup istenen değerleri atadık. Return ile döndük. 333 | } 334 | 335 | /* Method 2 */ 336 | function Rectangle(a, b) { 337 | this.length = a; 338 | this.width = b; 339 | this.perimeter = 2 * (a + b); 340 | this.area = a * b; 341 | } 342 | 343 | /************** Day:4 Count Object **************/ 344 | 345 | /* Method 1 */ 346 | function getCount(objects) { 347 | for (let p in objects) { 348 | return objects.filter((item) => item.x == item.y).length; 349 | } 350 | } 351 | 352 | /* Method 2 */ 353 | function getCount(objects) { 354 | let count = 0; 355 | 356 | for (let i = 0; i < objects.length; i++) { 357 | if (objects[i].x == objects[i].y) { 358 | // objects'de x'in (y'nin) i. index'i. 359 | count++; 360 | } 361 | } 362 | return count; 363 | } 364 | 365 | /* Method 3 */ 366 | function getCount(objects) { 367 | let count = 0; 368 | 369 | for (let index in objects) { 370 | if (objects[index].x == objects[index].y) { 371 | count++; 372 | } 373 | } 374 | return count; 375 | } 376 | 377 | /************** Day:4 Classes**************/ 378 | 379 | class Polygon { 380 | constructor(sides) { 381 | this.sides = sides; 382 | } 383 | 384 | perimeter() { 385 | let sum = 0; 386 | 387 | for (let i = 0; i < this.sides.length; i++) { 388 | sum = sum + this.sides[i]; 389 | } 390 | return sum; 391 | } 392 | } 393 | 394 | /************** Day:5 Inheritance**************/ 395 | Rectangle.prototype.area = function () { 396 | return this.w * this.h; 397 | }; 398 | 399 | class Square extends Rectangle { 400 | constructor(s) { 401 | super(); 402 | this.h = s; 403 | this.w = s; 404 | } 405 | } 406 | //rectangel superclass, square subclass. square rectangle'dan extends ile inharitance (miras alma) yaptı. 407 | 408 | /************** Day:5 Template Literals **************/ 409 | 410 | /* Method 1 */ 411 | function sides(literals, ...expressions) { 412 | let Area = expressions[0]; 413 | let Perimeter = expressions[1]; 414 | 415 | let s1 = (Perimeter + islem(Area, Perimeter)) / 4; // yukaridaki islemi kod'a döktük. 416 | let s2 = (Perimeter - islem(Area, Perimeter)) / 4; 417 | return [s2, s1]; // kücükten büyüge sirala 418 | } 419 | 420 | function islem(Area, Perimeter) { 421 | return Math.sqrt(Math.pow(Perimeter, 2) - 16 * Area); // yukarıdaki islemi kod'a döktük. 422 | } 423 | 424 | /* Method 2 */ 425 | function sides(literals, ...expressions) { 426 | var area = expressions[0]; 427 | var perimeter = expressions[1]; 428 | 429 | var s1 = (perimeter + Math.sqrt(perimeter * perimeter - 16 * area)) / 4; //tüm islemi tek satırda yaptık. 430 | //console.log("s1: " + s1); 431 | var s2 = (perimeter - Math.sqrt(perimeter * perimeter - 16 * area)) / 4; 432 | //console.log("s2: " + s2); 433 | var array = [s1, s2]; 434 | array = array.sort(function (a, b) { 435 | return a - b; 436 | }); //sıralamayı yaptık 437 | return array; 438 | } 439 | 440 | /* Method 3 */ 441 | function sides(literals, ...expressions) { 442 | const [a, p] = expressions; 443 | const root = Math.sqrt(p * p - 16 * a); 444 | const s1 = (p + root) / 4; 445 | const s2 = (p - root) / 4; 446 | 447 | return [s2, s1]; 448 | } 449 | 450 | /************** Day:5 Arrow Functions **************/ 451 | 452 | /* Method 1 */ 453 | function modifyArray(nums) { 454 | //array dondurduk 455 | for (let i = 0; i < nums.length; i++) { 456 | //eger citse 2 ile carpacak 457 | if (nums[i] % 2 === 0) { 458 | nums[i] *= 2; 459 | //eger tek ise 3 ile carpacak 460 | } else { 461 | nums[i] *= 3; 462 | } 463 | } 464 | //return Array 465 | return nums; 466 | }; 467 | 468 | /* Method 2 */ 469 | function modifyArray(nums) { 470 | const func = nums.map(function(num) { // for yerine map ile donduk 471 | 472 | if (num % 2 == 0){ 473 | return 2*num; 474 | }else{ 475 | return 3*num; 476 | } 477 | }); 478 | return func; 479 | }; 480 | 481 | /* Method 3 */ 482 | function modifyArray(nums) { 483 | let Array1 = function(n) { 484 | if(n % 2 == 0) 485 | return n * 2 // tek satırlı kodda süslü parantez kullanmayabiliriz. 486 | else 487 | return n * 3 488 | } 489 | let Array2 = nums.map(Array1); // nums dizisinin içindeki bilgileri al ve Array1 değişkenine ata. map() yardımıyla Array1'e atadığın fonksiyonu kullanarak yeni bir Array2 dizisi elde et. map(); array'i tek tek döner ve yeni bir array olusturur. 490 | return Array2 491 | } 492 | 493 | 494 | /* Method 4 */ 495 | function modifyArray(nums) { 496 | let Array1 = n => (n % 2 == 0) ? n * 2: n * 3 //arrow func ve ternary operator. 497 | let Array2 = nums.map(Array1) 498 | return Array2 499 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Reyhan Yılmaz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank-10DaysOfJavaScript 2 | HackerRank - 10 Days of JavaScript Tutorial 3 | 4 | Bu repository tek JS dosyası içerisinde gün gün olarak "10 Days of JavaScript" çözümleri içermektedir. 5 | 6 | --------------------------------------------------------------------------------