JavaScript basic - Exercises, Practice, Solution
JavaScript basic [150 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Display Current Day and Time
Write a JavaScript program to display the current day and time in the following format.
Sample Output : Today is : Tuesday.
Current time is : 10 PM : 30 : 38
Click me to see the solution
2. Print Current Window Contents
Write a JavaScript program to print the current window contents.
Click me to see the solution
3. Get Current Date in Various Formats
Write a JavaScript program to get the current date.
Expected Output :
mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyy
Click me to see the solution
4. Calculate Area of Triangle (Sides: 5, 6, 7)
Write a JavaScript program to find the area of a triangle where three sides are 5, 6, 7.
Click me to see the solution
5. Rotate String 'w3resource' Periodically
Write a JavaScript program to rotate the string 'w3resource' in the right direction. This is done by periodically removing one letter from the string end and attaching it to the front.
Click me to see the solution
6. Check Leap Year (Gregorian Calendar)
Write a JavaScript program to determine whether a given year is a leap year in the Gregorian calendar.
Click me to see the solution
7. Find Years When Jan 1 is Sunday (2014?2050)
Write a JavaScript program to find out if 1st January will be a Sunday between 2014 and 2050.
Click me to see the solution
8. Random Integer Guess Game
Write a JavaScript program where the program takes a random integer between 1 and 10, and the user is then prompted to input a guess number. The program displays a message "Good Work" if the input matches the guess number otherwise "Not matched".
Click me to see the solution
9. Days Left Before Christmas
Write a JavaScript program to calculate the days left before Christmas.
Click me to see the solution
10. Multiplication and Division (User Input)
Write a JavaScript program to calculate multiplication and division of two numbers (input from the user).
Sample form :
Click me to see the solution
11. Convert Temperatures Between Celsius and Fahrenheit
Write a JavaScript program to convert temperatures to and from
Celsius, Fahrenheit.
[ Formula : c/5 = (f-32)/9 [ where c = temperature in Celsius and f = temperature in
Fahrenheit ]
Expected Output :
60°C is 140 °F
45°F is 7.222222222222222°C
Click me to see the solution
12. Get Current Website URL
Write a JavaScript program to get the website URL (loading page).
Click me to see the solution
13. Create Variable with User-Defined Name
Write a JavaScript exercise to create a variable using a user-defined name.
Click me to see the solution
14. Get File Extension of Filename
Write a JavaScript exercise to get the filename extension.
Click me to see the solution
15. Difference Between Number and 13
Write a JavaScript program to get the difference between a given number and 13, if the number is broader than 13 return double the absolute difference.
Click me to see the solution
16. Sum Two Integers (Triple if Equal)
Write a JavaScript program to compute the sum of the two given integers. If the two values are the same, then return triple their sum.
Click me to see the solution
17. Difference Between Number and 19 (Triple if >19)
Write a JavaScript program to compute the absolute difference between a specified number and 19. Returns triple the absolute difference if the specified number is greater than 19.
Click me to see the solution
18. Check if Number or Sum is 50
Write a JavaScript program to check a pair of numbers and return true if one of the numbers is 50 or if their sum is 50.
Click me to see the solution
19. Check if Integer is Within 20 of 100 or 400
Write a JavaScript program to check whether a given integer is within 20 of 100 or 400.
Click me to see the solution
20. Check if One Integer is Positive and One is Negative
Write a JavaScript program to check two given integers whether one is positive and another one is negative.
Click me to see the solution
21. Add 'Py' to Start of String if Not Present
Write a JavaScript program to create another string by adding "Py" in front of a given string. If the given string begins with "Py" return the original string.
Click me to see the solution
22. Remove Character at Specified Position in String
Write a JavaScript program to remove a character at the specified position in a given string and return the modified string.
Click me to see the solution
23. Swap First and Last Characters in String
Write a JavaScript program to create a new string from a given string by changing the position of the first and last characters. The string length must be broader than or equal to 1.
Click me to see the solution
24. Add First Character to Front and Back of String
Write a JavaScript program to create another string from a given string with the first character of the given string added to the front and back.
Click me to see the solution
25. Check if Number is Multiple of 3 or 7
Write a JavaScript program to check whether a given positive number is a multiple of 3 or 7.
Click me to see the solution
26. Add Last 3 Characters to Front and Back of String
Write a JavaScript program to create a string from a given string. This is done by taking the last 3 characters and adding them at both the front and back. The string length must be 3 or more.
Click me to see the solution
27. Check if String Starts with 'Java'
Write a JavaScript program to check whether a string starts with 'Java' if it does not otherwise.
Click me to see the solution
28. Check if Two Integers are in Range 50?99
Write a JavaScript program to check whether two given integer values are in the range 50..99 (inclusive). Return true if either of them falls within the range.
Click me to see the solution
29. Check if Three Integers are in Range 50?99
Write a JavaScript program to check whether three given integer values are in the range 50..99 (inclusive). Return true if one or more of them are in the specified range.
Click me to see the solution
30. Remove 'Script' from String at 5th Position
Write a JavaScript program to check whether a string "Script" appears at the 5th (index 4) position in a given string. If "Script" appears in the string, return the string without "Script" otherwise return the original one.
Click me to see the solution
31. Find Largest of Three Integers
Write a JavaScript program to find the largest of three given integers.
Click me to see the solution
32. Find Closest Value to 100 from Two Numbers
Write a JavaScript program to find the closest value to 100 from two numerical values.
Click me to see the solution
33. Check if Two Numbers are in Specific Ranges
Write a JavaScript program to check whether two numbers are in the range 40..60 or 70..100 inclusive.
Click me to see the solution
34. Find Larger Number in Range 40?60
Write a JavaScript program to find the largest number from the two given positive integers. The two numbers are in the range 40..60 inclusive.
Click me to see the solution
35. Check Character Between 2nd and 4th Positions in String
Write a program to check whether a specified character exists between the 2nd and 4th positions in a given string.
Click me to see the solution
36. Check if Last Digit of Three Integers is Same
Write a JavaScript program that checks whether the last digit of three positive integers is the same.
Click me to see the solution
37. Modify String Based on Length (First 3 Lowercase/Uppercase)
Write a JavaScript program to produce a new string that has the first 3 characters in lower case from a given string. If the string length is less than 3 convert all the characters to upper case.
Click me to see the solution
38. Evaluate Grades Based on Total Marks and Final Exam
Write a JavaScript program to evaluate a student's total marks across various examinations and determine their grade. The grading criteria are as follows:
- If the total marks fall within the range of 89 to 100 (inclusive), the student receives an A+ grade.
- If the examination is labeled as "Final-exam," the student will receive an A+ grade only if their total marks are 90 or greater. Assume that final examination means we pass 'true' as second parameter otherwise blank.
- If the student achieves an A+ grade, the program should return 'true'; otherwise, it should return 'false'.
Click me to see the solution
39. Sum Two Integers and Return Based on Range
Write a JavaScript program to compute the sum of the two given integers. If the sum is in the range 50..80 return 65 otherwise return 80.
Click me to see the solution
40. Check if Integer is 8 or Difference/Sum Equals 8
Write a JavaScript program to check from two given integers whether one of them is 8 or their sum or difference is 8.
Click me to see the solution
41. Return 30, 40, or 20 Based on Same Numbers
Write a JavaScript program to check a set of three numbers; if the three numbers are the same return 30; otherwise return 20; and if two numbers are the same return 40.
Click me to see the solution
42. Check Numbers in Strict or Soft Increasing Mode
Write a JavaScript program to check whether three given numbers are increasing in strict or in soft mode.
Note: Strict mode -> 10, 15, 31 : Soft mode -> 24, 22, 31 or 22, 22, 31
Click me to see the solution
43. Check Rightmost Digits of Three Numbers
Write a JavaScript program to check from three given numbers (non negative integers) that two or all of them have the same rightmost digit.
Click me to see the solution
44. Evaluate if Integer is =20 and Less Than Another
Write a JavaScript program that evaluates three given integers to determine if any one of them is greater than or equal to 20 and less than at least one of the other two.
Click me to see the solution
45. Check if Integer is 15, or Sum/Difference is 15
Write a JavaScript program that checks two integer values and returns true if either one is 15 or if their sum or difference is 15.
Click me to see the solution
46. Check if Only One Integer is Multiple of 7 or 11
Write a JavaScript program to check two given non-negative integers if one (not both) is a multiple of 7 or 11.
Click me to see the solution
47. Check if Integer is in Range 40?10,000
Write a JavaScript program to check whether a given number exists in the range 40..10000.
For example 40 presents in 40 and 4000
Click me to see the solution
48. Reverse a Given String
Write a JavaScript program to reverse a given string.
Click me to see the solution
49. Replace Each Character with Next Alphabet Letter
Write a JavaScript program to replace every character in a given string with the character following it in the alphabet.
Click me to see the solution
50. Capitalize First Letter of Each Word in String
Write a JavaScript program to capitalize the first letter of each word in a given string.
Click me to see the solution
51. Transform Number to Hours and Minutes
Write a JavaScript application that transforms a provided numerical value into hours and minutes.
Click me to see the solution
52. Sort Letters Alphabetically in String
Write a JavaScript program to convert letters of a given string alphabetically.
Click me to see the solution
53. Check 'a' and 'b' Separated by Exactly 3 Places
Write a JavaScript program to check whether the characters a and b are separated by exactly 3 places anywhere (at least once) in a given string.
Click me to see the solution
54. Count Vowels in String
Write a JavaScript program to count the number of vowels in a given string.
Click me to see the solution
55. Check Equal Number of 'p's and 't's
Write a JavaScript program to check whether a given string contains an equal number of p's and t's.
Click me to see the solution
56. Divide Numbers and Format with Commas
Write a JavaScript program to divide two positive numbers and return the result as string with properly formatted commas.
Click me to see the solution
57. Create String of Specified Copies
Write a JavaScript program to create one string of specified copies (positive numbers) of a given string.
Click me to see the solution
58. Four Copies of Last 3 Characters
Write a JavaScript program to create an updated string of 4 copies of the last 3 characters of a given original string. The string length must be 3 and above.
Click me to see the solution
59. Extract First Half of Even-Length String
Write a JavaScript program to extract the first half of a even string.
Click me to see the solution
60. Remove First and Last Characters in String
Write a JavaScript program to create a new string without the first and last characters of a given string.
Click me to see the solution
61. Concatenate Two Strings Without First Character
Write a JavaScript program to concatenate two strings except for their first character.
Click me to see the solution
62. Move Last 3 Characters to Start of String
Write a JavaScript program to move the last three characters to the start of a given string. The string length must be greater than or equal to three.
Click me to see the solution
63. Extract Middle 3 Characters from Odd-Length String
Write a JavaScript program to create a string using the middle three characters of a given string of odd length. The string length must be greater than or equal to three.
Click me to see the solution
64. Concatenate Strings with Matching Length
Write a JavaScript program to concatenate two strings and return the result. If the length of the strings does not match, then remove the characters from the longer string.
Click me to see the solution
65. Check if String Ends with 'Script'
Write a JavaScript program to test whether a string ends with "Script". The string length must be greater than or equal to 6.
Click me to see the solution
66. Return City Name if Starts with 'Los' or 'New'
Write a JavaScript program to display the city name if the string begins with "Los" or "New" otherwise return blank.
Click me to see the solution
67. Remove First/Last 'P' Characters in String
Write a JavaScript program to create a new string from a given string. This program removes the first and last characters of the string if the first or last character is 'P'. Return the original string if the condition is not satisfied.
Click me to see the solution
68. Use First and Last 'n' Characters from String
Write a JavaScript program to create a new string using the first and last n characters from a given string. The string length must be larger than or equal to n.
Click me to see the solution
69. Sum of 3 Elements in Array (Length 3)
Write a JavaScript program to compute the sum of three elements of a given array of integers of length 3.
Click me to see the solution
70. Rotate Elements Left in Array (Length 3)
Write a JavaScript program to rotate the elements left in a given array of integers of length 3.
Click me to see the solution
71. Check if 1 is First/Last Element in Array
Write a JavaScript program to check whether 1 appears in the first or last position of a given array of integers. The array length must be larger than or equal to 1.
Click me to see the solution
72. Check if First and Last Elements Are Same
Write a JavaScript program to check whether the first and last elements are the same in a given array of integers of length 3.
Click me to see the solution
73. Reverse Elements of Array (Length 3)
Write a JavaScript program to reverse the elements of a given array of integers of length 3.
Click me to see the solution
74. Set All Elements to Largest of First/Last in Array
Write a JavaScript program to find the largest value between the first and last elements and set all the other elements to that value. Display the updated array.
Click me to see the solution
75. Create Array with Middle Elements from Two Arrays
Write a JavaScript program to create an array taking the middle elements of the two arrays of integer and each length 3.
Click me to see the solution
76. Create Array with First/Last Elements from Array
Write a JavaScript program to create an array by taking the first and last elements from a given array of integers. The length must be larger than or equal to 1.
Click me to see the solution
77. Check if Array Contains 1 or 3
Write a JavaScript program to test whether an array of integers of length 2 contains 1 or 3.
Click me to see the solution
78. Check if Array Does Not Contain 1 or 3
Write a JavaScript program to test whether an array of integers of length 2 does not contain 1 or 3.
Click me to see the solution
79. Check if Array Contains 30 and 40 Twice
Write a JavaScript program to test whether a given array of integers contains 30 and 40 twice. The array length should be 0, 1, or 2.
Click me to see the solution
80. Swap First and Last Elements in Array
Write a JavaScript program to swap the first and last elements of a given array of integers. The array length should be at least 1.
Click me to see the solution
81. Add Two Digits in a Two-Digit Number
Write a JavaScript program to add two digits to a given positive integer of length two.
Click me to see the solution
82. Add Two Integers Without Carrying
Write a JavaScript program to add two positive integers without carrying.
Click me to see the solution
83. Find Longest String in Array
Write a JavaScript program to find the longest string from a given array of strings.
Click me to see the solution
84. Replace Characters with Next in Alphabet
Write a JavaScript program to replace each character in a given string with the next in the English alphabet.
Note: 'a' will be replace by 'b' or 'z' would be replaced by 'a'.
Click me to see the solution
85. Split Array into Two Sums Alternating Elements
Write a JavaScript program to divide a given array of positive integers into two parts. First element belongs to the first part, second element belongs to the second part, and third element belongs to the first part and so on. Now compute the sum of two parts and store it in an array of size two.
Click me to see the solution
86. Find Type of Given Angle
Write a JavaScript program to find the types of a given angle.
- Types of angles:
- Acute angle: An angle between 0 and 90 degrees.
- Right angle: An 90 degree angle.
- Obtuse angle: An angle between 90 and 180 degrees.
- Straight angle: A 180 degree angle.
87. Check if Two Arrays Are Similar with One Swap
Write a JavaScript program to determine if two arrays of integers of the same length are similar. The arrays will be similar if one array can be obtained from another array by swapping at most one pair of elements.
Click me to see the solution
88. Check Similarity of Two Integers with Divisor
Write a JavaScript program that takes two integers and a divisor. If the given divisor divides both integers and does not divide either, two specified integers are similar. Check whether two integers are similar or not.
Click me to see the solution
89. Replace $ in Expression to Make True
Write a JavaScript program to check whether it is possible to replace $ in a given expression x $ y = z with one of the four signs +, -, * or / to obtain a correct expression.
For example x = 10, y = 30 and z = 300, we can replace $ with a multiple operator (*) to obtain x * y = z
Click me to see the solution
90. Find kth Greatest Element in Array
Write a JavaScript program to find the kth greatest element in a given array of integers.
Click me to see the solution
91. Find Max Sum of k Consecutive Numbers in Array
Write a JavaScript program to find the maximum possible sum of some of its k consecutive numbers (numbers that follow each other in order) in a given array of positive integers.
Click me to see the solution
92. Find Max Difference Between Adjacent Elements
Write a JavaScript program to find the maximum difference between any two adjacent elements of a given array of integers.
Click me to see the solution
93. Find Max Difference Among All Pairs in Array
Write a JavaScript program to find the maximum difference among all possible pairs of a given array of integers.
Click me to see the solution
94. Find Most Frequent Number in Array
Write a JavaScript program to find the number appearing most frequently in a given array of integers.
Click me to see the solution
95. Replace Numbers with Specified Value in Array
Write a JavaScript program to replace all numbers with a specified number in an array of integers.
Click me to see the solution
96. Sum of Absolute Differences of Consecutive Numbers
Write a JavaScript program to compute the sum of the absolute differences of consecutive numbers in a given array of integers.
Click me to see the solution
97. Shortest String to Convert into Palindrome
Write a JavaScript program to find the shortest possible string. This can be converted into a string and converted into a palindrome by adding characters to the end of it.
Click me to see the solution
98. Change Case Minimally for Upper or Lower Case
Write a JavaScript program to change the case of the minimum number of letters to make a given string written in upper case or lower case.
Fox example "Write" will be write and "PHp" will be "PHP"
Click me to see the solution
99. Check if String Can Rearrange to Match Another
Write a JavaScript program to check whether it is possible to rearrange the characters of a given string. This is in such a way that it will become equal to another given string.
Click me to see the solution
100. Check if Arrays Share at Least One Common Element
Write a JavaScript program to check if there is at least one element in two given sorted arrays of integers.
Click me to see the solution
101. Check Latin Letters with No Adjacent Upper/Lower Case
Write a JavaScript program to check whether a given string contains only Latin letters and no two uppercase and no two lowercase letters are in adjacent positions.
Click me to see the solution
102. Count Inversions in Array
Write a JavaScript program to find the number of inversions of a given array of integers.
Note: Two elements of the array a stored at positions i and j form an inversion if a[i] > a[j] and i < j.
Click me to see the solution
103. Max Integer by Removing One Digit
Write a JavaScript program to find the maximum number of a given positive integer by deleting exactly one digit of the given number.
Click me to see the solution
104. Find Pair with Closest Absolute Difference
Write a JavaScript program to find two elements of an array such that their absolute difference is not larger than a given integer. However, it is as close as possible to the integer.
Click me to see the solution
105. Replace Number with Digit Sum Until Single Digit
Write a JavaScript program to find the number of times to replace a given number with the sum of its digits. This is until it converts to a single-digit number.
Click me to see the solution
106. Divide Integers Until Result is Integer
Write a JavaScript program to divide an integer by another integer as long as the result is an integer and return the result.
Click me to see the solution
107. Count Divisible Sorted Pairs in Array
Write a JavaScript program to find the number of sorted pairs formed by arrays of integers. This is such that one element in the pair is divisible by the other one.
For example - The output of [1, 3, 2] ->2 - (1,3), (1,2).
The output of [2, 4, 6] -> 2 - (2,4), (2,6)
The output of [2, 4, 16] -> 3 - (2,4), (2,16), (4,16)
Click me to see the solution
108. Dot Product of Two 3D Vectors
Write a JavaScript program to create the dot products of two given 3D vectors.
Note: The dot product is the sum of the products of the corresponding entries of the two sequences of numbers.
Click me to see the solution
109. Sort All Primes Between 1 and n
Write a JavaScript program to sort an array of all prime numbers between 1 and a given integer.
Click me to see the solution
110. Count Evens Before First Occurrence of Number
Write a JavaScript program to find the number of even values in sequence before the first occurrence of a given number.
Click me to see the solution
111. Find Unique Number Among Three
Write a JavaScript program to check a number from three given numbers where two numbers are equal. Find the third one.
Click me to see the solution
112. Count Trailing Zeros in Factorial
Write a JavaScript program to find the number of trailing zeros in the decimal representation of the factorial of a given number.
Click me to see the solution
113. Sum n + n/2 + n/4 + ...
Write a JavaScript program to calculate the sum of n + n/2 + n/4 + n/8 + .... where n is a positive integer and all divisions are integers.
Click me to see the solution
114. Check String as Correct Sentence
Write a JavaScript program to check whether a given string represents a correct sentence or not. A string is considered a correct sentence if it starts with a capital letter and ends with a full stop (.)
Click me to see the solution
115. Check if Matrix is Diagonal Matrix
Write a JavaScript program to check whether a matrix is a diagonal matrix or not. In linear algebra, a diagonal matrix is a matrix in which the entries outside the main diagonal are all zero (the diagonal from the upper left to the lower right).
Example:
[1, 0, 0], [0, 2, 0], [0, 0, 3] ]) = true
[1, 0, 0], [0, 2, 3], [0, 0, 3] ]) = false
Click me to see the solution
116. Replace Hash in String to Make Divisible by 3
Write a JavaScript program to find all the possible options to replace the hash in a string (Consists of digits and one hash (#)) with a digit to produce an integer divisible by 3.
For a string "2*0", the output should be : ["210", "240", "270"]
Click me to see the solution
117. Check if Matrix is Identity Matrix
Write a JavaScript program to check whether a given matrix is an identity matrix.
Note: In linear algebra, the identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n ? n square matrix with ones on the main diagonal and zeros elsewhere.
[[1, 0, 0], [0, 1, 0], [0, 0, 1]] -> true
[[1, 0, 0], [0, 1, 0], [1, 0, 1]] -> false
Click me to see the solution
118. Check if Number is in Range
Write a JavaScript program to check whether a given number is in a given range.
Click me to see the solution
119. Check if Digits in Integer Are Increasing
Write a JavaScript program to check if a given integer has an increasing digit sequence.
Click me to see the solution
120. Check if Point is Inside Circle
Write a JavaScript program to check if a point lies strictly inside the circle.
Input:
Center of the circle (x, y)
Radius of circle: r
Point inside a circle (a, b)
Click me to see the solution
121. Check if Matrix is Lower Triangular
Write a JavaScript program to check whether a given matrix is lower triangular or not.
Note: A square matrix is called lower triangular if all the entries above the main diagonal are zero.
Click me to see the solution
122. Check if Array is Strictly Increasing/Decreasing
Write a JavaScript program to check whether a given array of integers represents a strictly increasing or decreasing sequence.
Click me to see the solution
123. Check if Array is Permutation of Numbers 1 to n
Write a JavaScript program to find out if the members of a given array of integers are a permutation of numbers from 1 to a given integer.
Click me to see the solution
124. Compute NOR of Two Booleans
Write a JavaScript program to create the NOR value of two given booleans.
Note: In boolean logic, logical nor or joint denial is a truth-functional operator which produces a result that is the negation of logical or. That is, a sentence of the form (p NOR q) is true precisely when neither p nor q is true - i.e. when both of p and q are false
Sample Example:
For x = true and y = false, the output should be logical_Nor(x, y) = false; For x = false and y = false, the output should be logical_Nor(x, y) = true.
125. Find Longest String in Array
Write a JavaScript program to find the longest string in a given array.
Click me to see the solution
126. Find Largest Even Number in Array
Write a JavaScript program to get the largest even number from an array of integers.
Click me to see the solution
127. Reverse Bits in Integer
Write a JavaScript program to reverse the order of bits in a integer.
14 -> 00001110 -> 01110000 -> 112
56 -> 00111000 -> 00011100 -> 28
234 -> 11101010 -> 01010111 -> 87
Click me to see the solution
128. Find Smallest Round Number = Value
Write a JavaScript program to find the smallest round number not less than a given value.
Note: A round number is informally considered to be an integer that ends with one or more zeros.[3] So, 590 is rounder than 592, but 590 is less round than 600.
Click me to see the solution
129. Find Smallest Prime > Value
Write a JavaScript program to find the smallest prime number strictly greater than a given number.
Click me to see the solution
130. Count Even Digits in Integer
Write a JavaScript program to find the number of even digits in a given integer.
Click me to see the solution
131. Create Prefix Sum Array
Write a JavaScript program to create an array of prefix sums of the given array.
In computer science, the prefix sum, cumulative sum, inclusive scan, or simply scan of a sequence of numbers x0, x1, x2, ... is a second sequence of numbers y0, y1, y2, ..., the sums of prefixes of the input sequence:
y0 = x0
y1 = x0 + x1
y2 = x0 + x1+ x2
...
Click me to see the solution
132. Find Distinct Prime Factors of Integer
Write a JavaScript program to find all distinct prime factors of a given integer.
Click me to see the solution
133. Check if Fraction is Proper
Write a JavaScript program to check whether a given fraction is proper or not.
Note: There are two types of common fractions, proper or improper. When the numerator and the denominator are both positive, the fraction is called proper if the numerator is less than the denominator, and improper otherwise.
Click me to see the solution
134. Reverse Alphabetical Order of Lowercase Letters
Write a JavaScript program to change the characters (lower case) in a string where a turns into z, b turns into y, c turns into x, ..., n turns into m, m turns into n, ..., z turns into a.
Click me to see the solution
135. Remove Characters Appearing More Than Once
Write a JavaScript program to remove all characters from a given string that appear more than once.
Click me to see the solution
136. Replace First Digit in String with $
Write a JavaScript program to replace the first digit in a string (should have at least one digit) with the $ character.
Click me to see the solution
137. Return Number if >15, Else 15
Write a JavaScript program to test whether a given integer is greater than 15 and return the given number, otherwise return 15.
Click me to see the solution
138. Reverse Bits in 16-Bit Unsigned Integer
Write a JavaScript program to reverse the bits of a given 16-bit unsigned short integer.
Click me to see the solution
139. Find Position of Rightmost Round Number
Write a JavaScript program to find the position of the rightmost round number in an array of integers. If there are no round numbers, the function returns 0.
Note: A round number is informally considered to be an integer that ends with one or more zeros.
Click me to see the solution
140. Check if All Digits in Number Are Same
Write a JavaScript program to check whether all the digits in a given number are the same or not.
Click me to see the solution
141. Find Number of Elements in Both Arrays
Write a JavaScript program to find the number of elements in both arrays.
Click me to see the solution
142. Simplify Unix-Style Absolute File Path
Write a JavaScript program to simplify a given absolute path for a file in Unix-style.
Click me to see the solution
143. Sort Strings by Increasing Length
Write a JavaScript program to sort the strings of a given array of strings in order of increasing length.
Note: Do not change the order if the lengths of two string are same.
Click me to see the solution
144. Break URL into Parts
Write a JavaScript program to break an URL address and put its parts into an array.
Note: url structure : ://.org[/] and there may be no part in the address.
Click me to see the solution
145. Max n for 1+2+...+n = Value
Write a JavaScript program to find the maximum integer n such that 1 + 2 + ... + n <= a given integer.
Click me to see the solution
146. Sum of Cubes from 1 to n
Write a JavaScript program to compute the sum of cubes of all integers from 1 to a given integer.
Click me to see the solution
147. Sum of Digits in String
Write a JavaScript program to compute the sum of all the digits that occur in a given string.
Click me to see the solution
148. Swap Halves of Even-Length Array
Write a JavaScript program to swap two halves of a given array of integers of even length.
Click me to see the solution
149. Change Capitalization of All Letters
Write a JavaScript program to change the capitalization of all letters in a given string.
Click me to see the solution
150. Swap Adjacent Pairs in Even-Length Integer
Write a JavaScript program to swap pairs of adjacent digits of a given integer of even length.
Click me to see the solution
More to Come !
Live Demo
* To run the code mouse over on Result panel and click on 'RERUN' button.*
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/javascript-exercises/javascript-basic-exercises.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics