Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A curated list of the best JavaScript practice exercises to get started.

License

NotificationsYou must be signed in to change notification settings

harshalslimaye/awesome-javascript-practice-exercises

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 

Repository files navigation

Click ⭐ if you like the project.

Table of Contents

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

functiongetTime(today){constampm=today.getHours()>12 ?'pm' :'am';consthours=today.getHours()%12 ?today.getHours()%12 :12;constminutes=today.getMinutes()<10 ?`0${today.getMinutes()}` :today.getMinutes();constseconds=today.getSeconds()<10 ?`0${today.getSeconds()}` :today.getSeconds();return`${hours}${ampm} :${minutes} :${seconds}`;}functiongetDay(today){return['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday',][today.getDay()];}constd=newDate();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:

functiongetDate(date,format,separator){constdata={yyyy:today.getFullYear(),mm:today.getMonth()<10 ?`0${today.getMonth()}` :today.getMonth(),dd:today.getDate()<10 ?`0${today.getDate()}` :today.getDate(),};returnformat.split(separator).map((char)=>data[char]).join(separator);}consttoday=newDate();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:

functionareaOfTriangle(a,b,c){consts=(a+b+c)/2;returnMath.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:

functiondaysUntilChristmas(){consttoday=newDate();constdifference=newDate(today.getFullYear(),11,25)-newDate();constoneDayInNilliseconds=1000*3600*24;returnMath.ceil(difference/oneDayInNilliseconds);}console.log(daysUntilChristmas());


⬆ Back to Top

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

Solution:

functiongetExtension(filename){returnfilename.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:

functionmatches(source,target){returnObject.keys(source).every((key)=>target.hasOwnProperty(key)&&target[key]===source[key]);}constcar={color:'red',type:'suv',};p1={name:'john doe',  car,};p2={name:'john doe',  car,};console.log(matches(p1,p2));// trueconsole.log(matches(p1,{color:'red',type:'suv'}));// falseconsole.log(matches(p1,{name:'john doe', car}));// trueconsole.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:

functionarrayToCSV(collection){constheaders={};constrows=collection.map((row)=>`${Object.keys(row).map((key)=>{headers[key]=key;returnrow[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:

functionnumberToArray(num){if(typeofnum==='number'){return`${num}`.split('').map((n)=>parseInt(n));}else{returnNaN;}}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:

functionucfirst(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:

functionis_array(param){returnObject.getPrototypeOf(param)===Array.prototype;}console.log(is_array([1,2,3,4]));// trueconsole.log(is_array('abcd'));// false


⬆ Back to Top

Write a JavaScript program to clone an array

Solution:

// using spread operatorfunctioncloneArr(arr){return[...arr];}console.log([1,2,3,4,5]);// using for slicefunctioncloneArr(arr){returnarr.slice();}console.log([1,2,3,4,5]);// using JSON objectfunctioncloneArr(arr){returnJSON.parse(JSON.stringify(arr));}console.log([1,2,3,4,5]);// using Array.fromfunctioncloneArr(arr){returnArray.from(arr);}console.log([1,2,3,4,5]);


⬆ Back to Top

Write a JavaScript program to reverse a string

Solution:

functionreverse(str){returnstr.split('').reverse().join('');}// Test the functionconstinput="Hello, World!";constreversed=reverse(input);console.log(reversed);// Output: "!dlroW ,olleH"


Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp