Skip to content

Commit 03d2c3b

Browse files
authored
Update primitives-challenges.md
1 parent 48bb0d1 commit 03d2c3b

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

challenges/primitives-challenges.md

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,11 @@
22
<a href="/README.md#this-is-a-collection-of-modern-interview-code-challenges-on-javascript-suitable-for" id="home">Home</a>
33
</div>
44

5-
## JavaScript interview code challenges on Primitives - challenges
6-
7-
1. [Swap 2 integers present in variables num1 and num2 without using temporary variable](#Q1)
8-
1. [Write a function which returns true if given value of number is an integer without using any inbuilt functions](#Q2)
9-
1. [Create a function which returns a random number in the given range of values both inclusive](#Q3)
10-
1. [Write a program to reverse a string](#Q4)
11-
1. [Write a program to reverse a string by words. Also show the reverse of each words in place](#Q5)
12-
1. [Write a program to reverse a given integer number](#Q6)
13-
1. [Write a code to replace all the spaces of the string with underscores](#Q7)
14-
1. [Write a function which can convert the time input given in 12 hours format to 24 hours format](#Q8)
15-
1. [Write a function which accepts a string argument and returns the count of characters between the first and last character 'X'](#Q9)
16-
1. [Write a function to truncate a string to a certain number of letters](#Q10)
17-
1. [Write a code to truncate a string to a certain number of words](#Q11)
18-
1. [Create a regular expression to validate if the given input is valid Indian mobile number or not](#Q12)
19-
1. [Write a function which returns a list of elements which contains at least one character as digit](#Q13)
20-
1. [Write a function which checks if a given search text is present either in the beginning of the first name or the second name](#Q14)
21-
1. [Write a function to chop a string into chunks of a given length and return it as array](#Q15)
22-
1. [Write a code to remove all the vowels from a given string](#Q16)
23-
1. [Create a function which returns random hex color code](#Q17)
24-
1. [Write a function which accepts two valid dates and returns the difference between them as number of days](#Q18)
25-
26-
---
27-
28-
#### Q1
29-
### Swap 2 integers present in variables num1 and num2 without using temporary variable
5+
<h2 align="center">JavaScript challenges on Primitives - challenges</h2>
6+
7+
<br>
8+
9+
### Q. Swap 2 integers present in variables num1 and num2 without using temporary variable
3010

3111
- The swapping of 2 variables is possible with simple Destructuring assignment using array
3212
- Traditional approach of swapping by using the given variables is also achievable
@@ -48,8 +28,7 @@ num1 = num1 - num2;
4828

4929
<br />
5030

51-
#### Q2
52-
### Write a function which returns true if given value of number is an integer without using any inbuilt functions
31+
### Q. Write a function which returns true if given value of number is an integer without using any inbuilt functions
5332
```js
5433
// Example
5534
isInt(4.0); // true
@@ -66,8 +45,7 @@ function isInt(value){
6645

6746
<br />
6847

69-
#### Q3
70-
### Create a function which returns a random number in the given range of values both inclusive
48+
### Q. Create a function which returns a random number in the given range of values both inclusive
7149

7250
- `Math.random` function returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive)
7351

@@ -87,8 +65,7 @@ Usage of `Math.round` depends on the logic used to accomplish the requirement
8765

8866
<br />
8967

90-
#### Q4
91-
### Write a program to reverse a string
68+
### Q. Write a program to reverse a string
9269

9370
- String can be reversed by iterating it and storing it in reverse order
9471
- String can also be reversed by converting it to array, then joining it after reversing
@@ -117,8 +94,7 @@ The string can be tested if it is __palindrome__, by comparing the actual string
11794
<br />
11895

11996

120-
#### Q5
121-
### Write a program to reverse a string by words. Also show the reverse of each words in place
97+
### Q. Write a program to reverse a string by words. Also show the reverse of each words in place
12298

12399
- The string can be reversed by words, by splitting the string with spaces and joining them back after reverse
124100
- If the the letters in each word have to be reversed, the string reversal procedure has to be followed after breaking the string with spaces
@@ -135,8 +111,7 @@ str.split(" ").map(val => val.split("").reverse().join("")).join(" ");
135111

136112
<br />
137113

138-
#### Q6
139-
### Write a program to reverse a given integer number
114+
### Q. Write a program to reverse a given integer number
140115

141116
- The remainder of the number can be fetched and the number can be divided by 10 to remvoe the the digit in loop till number becomes 0
142117
- A simple approach to reverse a number could also be to convert it in to a string and then reverse it
@@ -162,8 +137,7 @@ let numStr = String(num);
162137

163138
<br />
164139

165-
#### Q7
166-
### Write a code to replace all the spaces of the string with underscores
140+
### Q. Write a code to replace all the spaces of the string with underscores
167141

168142
- The string can be split using the space character and can be joined back with underscore to replace all the spaces with strings
169143
- `replaceAll` is the inbuilt String function on prototype which can be used to replace a string with another string
@@ -185,8 +159,7 @@ str.replaceAll(" ", "_");
185159

186160
<br />
187161

188-
#### Q8
189-
### Write a function which can convert the time input given in 12 hours format to 24 hours format
162+
### Q. Write a function which can convert the time input given in 12 hours format to 24 hours format
190163
```js
191164
// Example
192165
convertTo24HrsFormat("12:10AM"); // 00:10
@@ -223,8 +196,7 @@ Conversion of string to lowerCase helps in case insensitive comparision
223196

224197
<br />
225198

226-
#### Q9
227-
### Write a function which accepts a string argument and returns the count of characters between the first and last character 'X'
199+
### Q. Write a function which accepts a string argument and returns the count of characters between the first and last character 'X'
228200
```js
229201
// Example
230202
getTheGapX('XeroX'); // 4
@@ -250,8 +222,7 @@ function getTheGapX(str) {
250222

251223
<br />
252224

253-
#### Q10
254-
### Write a function to truncate a string to a certain number of letters
225+
### Q. Write a function to truncate a string to a certain number of letters
255226
```js
256227
// Example
257228
truncateString("JavaScript", 7) // "Java..."
@@ -274,8 +245,7 @@ function truncateString(str, charCount) {
274245

275246
<br />
276247

277-
#### Q11
278-
### Write a code to truncate a string to a certain number of words
248+
### Q. Write a code to truncate a string to a certain number of words
279249

280250
- The string can be broken in to words array and then `slice` method of array can be used to get the number of words which will then be joined back
281251

@@ -288,8 +258,7 @@ str.split(' ').slice(0, wordLimit).join(' '); // "JavaScript is si
288258

289259
<br />
290260

291-
#### Q12
292-
### Create a regular expression to validate if the given input is valid Indian mobile number or not
261+
### Q. Create a regular expression to validate if the given input is valid Indian mobile number or not
293262
```js
294263
// Example
295264
validateMobile('+919876543210'); // true
@@ -321,8 +290,7 @@ String has method `match` which returns array of matches or null
321290

322291
<br />
323292

324-
#### Q13
325-
### Write a function which returns a list of elements which contains at least one character as digit
293+
### Q. Write a function which returns a list of elements which contains at least one character as digit
326294
```js
327295
// Example
328296
numInStr(['1a', 'a', '2b', 'b'])); // ['1a', '2b']
@@ -343,8 +311,7 @@ function numInStr(mixArray){
343311

344312
<br />
345313

346-
#### Q14
347-
### Write a function which checks if a given search text is present either in the beginning of the first name or the second name
314+
### Q. Write a function which checks if a given search text is present either in the beginning of the first name or the second name
348315
```js
349316
// Example
350317
validateName('Nedson PETER', "pet"); // true
@@ -368,8 +335,7 @@ Case insensitive match is happening for the search text in the string represente
368335

369336
<br />
370337

371-
#### Q15
372-
### Write a function to chop a string into chunks of a given length and return it as array
338+
### Q. Write a function to chop a string into chunks of a given length and return it as array
373339
```js
374340
// Example
375341
stringChop('JavaScript'); // ["JavaScript"]
@@ -403,8 +369,7 @@ function stringChop(str, size = str.length) {
403369

404370
<br />
405371

406-
#### Q16
407-
### Write a code to remove all the vowels from a given string
372+
### Q. Write a code to remove all the vowels from a given string
408373

409374
- `replace` method on String accepts a string or regex as the argument
410375

@@ -418,8 +383,7 @@ str.replace(/[aeiou]/gi, ''); // _lv_JvScrpt
418383

419384
<br />
420385

421-
#### Q17
422-
### Create a function which returns random hex color code
386+
### Q. Create a function which returns random hex color code
423387

424388
- The color code is popularly represented in hexadecimal format for RGB colors
425389
- The minimum value for the color is '#000000' and maximum is '#FFFFFF'
@@ -441,8 +405,7 @@ function getGetHEXColorCode() {
441405

442406
<br />
443407

444-
#### Q18
445-
### Write a function which accepts two valid dates and returns the difference between them as number of days
408+
### Q. Write a function which accepts two valid dates and returns the difference between them as number of days
446409

447410
- The difference between 2 dates in JavaScript will give the time difference in milliseconds
448411
- Time difference can be converted in to days by dividing the 24Hrs time in milliseconds

0 commit comments

Comments
 (0)