Skip to content

harshalslimaye/awesome-javascript-practice-exercises

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 

Repository files navigation

🔥Awesome Javascript Practice Exercises

Click ⭐ if you like the project.

Table of Contents

No. Questions
1 Write a JavaScript program to display the current day and time in the following format
2 Write a JavaScript program to get the current date
3 Write a JavaScript program to find the area of a triangle
4 Write a JavaScript program to calculate days left until next Christmas
5 Write a JavaScript exercise to get the extension of a filename
6 Write a JavaScript program to compare two objects
7 Write a JavaScript program to convert an array of objects into CSV string
8 Write a JavaScript program to convert a number to array of digits
9 Write a JavaScript program to capitalize first letter of a string
10 Write a JavaScript program to determine if a variable is array
11 Write a JavaScript program to clone an array
12 Write a JavaScript program to reverse a string

Write a JavaScript program to display the current day and time in the following format

Sample Output :

Today is Friday.
Current time is 12 PM : 12 : 22

Solution:

function getTime(today) {
  const ampm = today.getHours() > 12 ? 'pm' : 'am';
  const hours = today.getHours() % 12 ? today.getHours() % 12 : 12;
  const minutes =
    today.getMinutes() < 10 ? `0${today.getMinutes()}` : today.getMinutes();
  const seconds =
    today.getSeconds() < 10 ? `0${today.getSeconds()}` : today.getSeconds();

  return `${hours} ${ampm} : ${minutes} : ${seconds}`;
}

function getDay(today) {
  return [
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
  ][today.getDay()];
}

const d = new Date();
console.log(`Today is ${getDay(d)}`);
console.log(`Current time is ${getTime(d)}`);


⬆ Back to Top

Write a JavaScript program to get the current date

mm-dd-yyyy
mm/dd/yyyy
dd-mm-yyyy
dd/mm/yyyy

Solution:

function getDate(date, format, separator) {
  const data = {
    yyyy: today.getFullYear(),
    mm: today.getMonth() < 10 ? `0${today.getMonth()}` : today.getMonth(),
    dd: today.getDate() < 10 ? `0${today.getDate()}` : today.getDate(),
  };

  return format
    .split(separator)
    .map((char) => data[char])
    .join(separator);
}
const today = new Date();

console.log(getDate(today, 'mm-dd-yyyy', '-'));
console.log(getDate(today, 'mm/dd/yyyy', '/'));
console.log(getDate(today, 'dd-mm-yyyy', '-'));
console.log(getDate(today, 'dd/mm/yyyy', '/'));


⬆ Back to Top

Write a JavaScript program to find the area of a triangle

Solution:

function areaOfTriangle(a, b, c) {
  const s = (a + b + c) / 2;

  return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}

console.log(areaOfTriangle(5, 6, 7));


⬆ Back to Top

Write a JavaScript program to calculate days left until next Christmas

Solution:

function daysUntilChristmas() {
  const today = new Date();
  const difference = new Date(today.getFullYear(), 11, 25) - new Date();
  const oneDayInNilliseconds = 1000 * 3600 * 24;

  return Math.ceil(difference / oneDayInNilliseconds);
}

console.log(daysUntilChristmas());


⬆ Back to Top

Write a JavaScript exercise to get the extension of a filename.

Solution:

function getExtension(filename) {
  return filename.substring(filename.lastIndexOf('.') + 1);
}

console.log(getExtension('hello-world.txt'));
console.log(getExtension('awesome.component.ts'));
console.log(getExtension('readme.md'));
console.log(getExtension('user.jsx'));


⬆ Back to Top

Write a JavaScript program to compare two objects

Solution:

function matches(source, target) {
  return Object.keys(source).every(
    (key) => target.hasOwnProperty(key) && target[key] === source[key]
  );
}

const car = {
  color: 'red',
  type: 'suv',
};

p1 = {
  name: 'john doe',
  car,
};
p2 = {
  name: 'john doe',
  car,
};
console.log(matches(p1, p2)); // true
console.log(matches(p1, { color: 'red', type: 'suv' })); // false
console.log(matches(p1, { name: 'john doe', car })); // true
console.log(matches(p1, { name: 'jane doe', car })); // false


⬆ Back to Top

Write a JavaScript program to convert an array of objects into CSV string

Solution:

function arrayToCSV(collection) {
  const headers = {};
  const rows = collection
    .map(
      (row) =>
        `${Object.keys(row)
          .map((key) => {
            headers[key] = key;

            return row[key];
          })
          .join(',')}`
    )
    .join('\n');

  return `${Object.keys(headers).join(',')}\n${rows}`;
}

console.log(
  arrayToCSV([
    { name: 'India', city: 'Pune', continent: 'Asia' },
    { name: 'Kenya', city: 'Mombasa', continent: 'Africa' },
    {
      name: 'Canada',
      city: 'Waterloo',
      continent: 'North America',
      captial: 'Ottawa',
    },
    { name: 'France', city: 'Paris', continent: 'Europe' },
  ])
);


⬆ Back to Top

Write a JavaScript program to convert a number to array of digits

Solution:

function numberToArray(num) {
  if (typeof num === 'number') {
    return `${num}`.split('').map((n) => parseInt(n));
  } else {
    return NaN;
  }
}

console.log(numberToArray(1234)); // [1, 2, 3, 4]
console.log(numberToArray('dsc')); // NaN


⬆ Back to Top

Write a JavaScript program to capitalize first letter of a string

Solution:

function ucfirst(str) {
  return `${str.charAt(0).toUpperCase()}${str.substring(1)}`;
}

console.log(ucfirst('javascript'));


⬆ Back to Top

Write a JavaScript program to determine if a variable is array

Solution:

function is_array(param) {
  return Object.getPrototypeOf(param) === Array.prototype;
}

console.log(is_array([1, 2, 3, 4])); // true
console.log(is_array('abcd')); // false


⬆ Back to Top

Write a JavaScript program to clone an array

Solution:

// using spread operator
function cloneArr(arr) {
  return [...arr];
}

console.log([1, 2, 3, 4, 5]);

// using for slice
function cloneArr(arr) {
  return arr.slice();
}

console.log([1, 2, 3, 4, 5]);

// using JSON object
function cloneArr(arr) {
  return JSON.parse(JSON.stringify(arr));
}

console.log([1, 2, 3, 4, 5]);

// using Array.from
function cloneArr(arr) {
  return Array.from(arr);
}

console.log([1, 2, 3, 4, 5]);


⬆ Back to Top

Write a JavaScript program to reverse a string

Solution:

function reverse(str) {
  return str.split('').reverse().join('');
}

// Test the function
const input = "Hello, World!";
const reversed = reverse(input);
console.log(reversed); 
// Output: "!dlroW ,olleH"