├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Harshal Limaye 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 | # 🔥Awesome Javascript Practice Exercises 2 | 3 | > Click :star: if you like the project. 4 | 5 | ### Table of Contents 6 | 7 | | No. | Questions | 8 | | --- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 9 | | 1 | [Write a JavaScript program to display the current day and time in the following format](#write-a-javascript-program-to-display-the-current-day-and-time-in-the-following-format) | 10 | | 2 | [Write a JavaScript program to get the current date](#write-a-javascript-program-to-get-the-current-date) | 11 | | 3 | [Write a JavaScript program to find the area of a triangle](#write-a-javascript-program-to-find-the-area-of-a-triangle) | 12 | | 4 | [Write a JavaScript program to calculate days left until next Christmas](#write-a-javascript-program-to-calculate-days-left-until-next-christmas) | 13 | | 5 | [Write a JavaScript exercise to get the extension of a filename](#write-a-javascript-exercise-to-get-the-extension-of-a-filename) | 14 | | 6 | [Write a JavaScript program to compare two objects](#write-a-javascript-program-to-compare-two-objects) | 15 | | 7 | [Write a JavaScript program to convert an array of objects into CSV string](#write-a-javascript-program-to-convert-an-array-of-objects-into-csv-string) | 16 | | 8 | [Write a JavaScript program to convert a number to array of digits](#write-a-javascript-program-to-convert-a-number-to-array-of-digits) | 17 | | 9 | [Write a JavaScript program to capitalize first letter of a string](#write-a-javascript-program-to-capitalize-first-letter-of-a-string) | 18 | | 10 | [Write a JavaScript program to determine if a variable is array](#write-a-javascript-program-to-determine-if-a-variable-is-array) | 19 | | 11 | [Write a JavaScript program to clone an array](#write-a-javascript-program-to-clone-an-array) | 20 | | 12 | [Write a JavaScript program to reverse a string](#write-a-javascript-program-to-reverse-a-string) | 21 | 22 | ### Write a JavaScript program to display the current day and time in the following format 23 | 24 | Sample Output : 25 | 26 |
27 | Today is Friday.
28 | Current time is 12 PM : 12 : 22
29 |
32 | 33 | ```javascript 34 | function getTime(today) { 35 | const ampm = today.getHours() > 12 ? 'pm' : 'am'; 36 | const hours = today.getHours() % 12 ? today.getHours() % 12 : 12; 37 | const minutes = 38 | today.getMinutes() < 10 ? `0${today.getMinutes()}` : today.getMinutes(); 39 | const seconds = 40 | today.getSeconds() < 10 ? `0${today.getSeconds()}` : today.getSeconds(); 41 | 42 | return `${hours} ${ampm} : ${minutes} : ${seconds}`; 43 | } 44 | 45 | function getDay(today) { 46 | return [ 47 | 'Sunday', 48 | 'Monday', 49 | 'Tuesday', 50 | 'Wednesday', 51 | 'Thursday', 52 | 'Friday', 53 | 'Saturday', 54 | ][today.getDay()]; 55 | } 56 | 57 | const d = new Date(); 58 | console.log(`Today is ${getDay(d)}`); 59 | console.log(`Current time is ${getTime(d)}`); 60 | ``` 61 | 62 |
63 |
79 | 80 | ```javascript 81 | function getDate(date, format, separator) { 82 | const data = { 83 | yyyy: today.getFullYear(), 84 | mm: today.getMonth() < 10 ? `0${today.getMonth()}` : today.getMonth(), 85 | dd: today.getDate() < 10 ? `0${today.getDate()}` : today.getDate(), 86 | }; 87 | 88 | return format 89 | .split(separator) 90 | .map((char) => data[char]) 91 | .join(separator); 92 | } 93 | const today = new Date(); 94 | 95 | console.log(getDate(today, 'mm-dd-yyyy', '-')); 96 | console.log(getDate(today, 'mm/dd/yyyy', '/')); 97 | console.log(getDate(today, 'dd-mm-yyyy', '-')); 98 | console.log(getDate(today, 'dd/mm/yyyy', '/')); 99 | ``` 100 | 101 |
102 |
113 | 114 | ```javascript 115 | function areaOfTriangle(a, b, c) { 116 | const s = (a + b + c) / 2; 117 | 118 | return Math.sqrt(s * (s - a) * (s - b) * (s - c)); 119 | } 120 | 121 | console.log(areaOfTriangle(5, 6, 7)); 122 | ``` 123 | 124 |
125 |
136 | 137 | ```javascript 138 | function daysUntilChristmas() { 139 | const today = new Date(); 140 | const difference = new Date(today.getFullYear(), 11, 25) - new Date(); 141 | const oneDayInNilliseconds = 1000 * 3600 * 24; 142 | 143 | return Math.ceil(difference / oneDayInNilliseconds); 144 | } 145 | 146 | console.log(daysUntilChristmas()); 147 | ``` 148 | 149 |
150 |
161 | 162 | ```javascript 163 | function getExtension(filename) { 164 | return filename.substring(filename.lastIndexOf('.') + 1); 165 | } 166 | 167 | console.log(getExtension('hello-world.txt')); 168 | console.log(getExtension('awesome.component.ts')); 169 | console.log(getExtension('readme.md')); 170 | console.log(getExtension('user.jsx')); 171 | ``` 172 | 173 |
174 |
185 | 186 | ```javascript 187 | function matches(source, target) { 188 | return Object.keys(source).every( 189 | (key) => target.hasOwnProperty(key) && target[key] === source[key] 190 | ); 191 | } 192 | 193 | const car = { 194 | color: 'red', 195 | type: 'suv', 196 | }; 197 | 198 | p1 = { 199 | name: 'john doe', 200 | car, 201 | }; 202 | p2 = { 203 | name: 'john doe', 204 | car, 205 | }; 206 | console.log(matches(p1, p2)); // true 207 | console.log(matches(p1, { color: 'red', type: 'suv' })); // false 208 | console.log(matches(p1, { name: 'john doe', car })); // true 209 | console.log(matches(p1, { name: 'jane doe', car })); // false 210 | ``` 211 | 212 |
213 |
224 | 225 | ```javascript 226 | function arrayToCSV(collection) { 227 | const headers = {}; 228 | const rows = collection 229 | .map( 230 | (row) => 231 | `${Object.keys(row) 232 | .map((key) => { 233 | headers[key] = key; 234 | 235 | return row[key]; 236 | }) 237 | .join(',')}` 238 | ) 239 | .join('\n'); 240 | 241 | return `${Object.keys(headers).join(',')}\n${rows}`; 242 | } 243 | 244 | console.log( 245 | arrayToCSV([ 246 | { name: 'India', city: 'Pune', continent: 'Asia' }, 247 | { name: 'Kenya', city: 'Mombasa', continent: 'Africa' }, 248 | { 249 | name: 'Canada', 250 | city: 'Waterloo', 251 | continent: 'North America', 252 | captial: 'Ottawa', 253 | }, 254 | { name: 'France', city: 'Paris', continent: 'Europe' }, 255 | ]) 256 | ); 257 | ``` 258 | 259 |
260 |
271 | 272 | ```javascript 273 | function numberToArray(num) { 274 | if (typeof num === 'number') { 275 | return `${num}`.split('').map((n) => parseInt(n)); 276 | } else { 277 | return NaN; 278 | } 279 | } 280 | 281 | console.log(numberToArray(1234)); // [1, 2, 3, 4] 282 | console.log(numberToArray('dsc')); // NaN 283 | ``` 284 | 285 |
286 |
297 | 298 | ```javascript 299 | function ucfirst(str) { 300 | return `${str.charAt(0).toUpperCase()}${str.substring(1)}`; 301 | } 302 | 303 | console.log(ucfirst('javascript')); 304 | ``` 305 | 306 |
307 |
318 | 319 | ```javascript 320 | function is_array(param) { 321 | return Object.getPrototypeOf(param) === Array.prototype; 322 | } 323 | 324 | console.log(is_array([1, 2, 3, 4])); // true 325 | console.log(is_array('abcd')); // false 326 | ``` 327 | 328 |
329 |
340 | 341 | ```javascript 342 | // using spread operator 343 | function cloneArr(arr) { 344 | return [...arr]; 345 | } 346 | 347 | console.log([1, 2, 3, 4, 5]); 348 | 349 | // using for slice 350 | function cloneArr(arr) { 351 | return arr.slice(); 352 | } 353 | 354 | console.log([1, 2, 3, 4, 5]); 355 | 356 | // using JSON object 357 | function cloneArr(arr) { 358 | return JSON.parse(JSON.stringify(arr)); 359 | } 360 | 361 | console.log([1, 2, 3, 4, 5]); 362 | 363 | // using Array.from 364 | function cloneArr(arr) { 365 | return Array.from(arr); 366 | } 367 | 368 | console.log([1, 2, 3, 4, 5]); 369 | ``` 370 | 371 |
372 |
379 | Solution:
380 |
395 | 396 | 397 | --- --------------------------------------------------------------------------------