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

Commit 607ee07

Browse files
committed
New JS questions added
1 parent 537e05f commit 607ee07

File tree

1 file changed

+287
-1
lines changed

1 file changed

+287
-1
lines changed

β€ŽREADME.md

Lines changed: 287 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Prepare for your next 2023 JavaScript interview with these tricky
44
githubPath: "https://github.com/Vasu7389/JavaScript-Interview-Questions-2023"
55
---
66

7-
<span style=" font-size: 0.8rem; border-bottom: 1px solid grey;"> Updated Feb 25, 2023 </span>
7+
<span style=" font-size: 0.8rem; border-bottom: 1px solid grey;"> Updated Mar 11, 2023 </span>
88

99
In this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
1010

@@ -605,3 +605,289 @@ The `sortStrings` function takes an array of strings as its parameter and uses t
605605
In the example usage, we pass an array of strings `['apple', 'banana', 'cherry', 'date', 'elderberry']` to the sortStrings function. The function returns a new array with the strings sorted in alphabetical order, which is `['apple', 'banana', 'cherry', 'date', 'elderberry']`.
606606

607607
</details>
608+
609+
<details>
610+
<summary>
611+
<h3>21. Write a function in JavaScript that checks if a string is a palindrome.
612+
</h3>
613+
</summary>
614+
Answer:
615+
616+
```js
617+
function isPalindrome(str) {
618+
const reversedStr = str.split("").reverse().join("");
619+
return str === reversedStr;
620+
}
621+
622+
// Example usage:
623+
console.log(isPalindrome("racecar")); // Output: true
624+
console.log(isPalindrome("hello")); // Output: false
625+
```
626+
627+
The `isPalindrome` function takes a string as its parameter and first reverses the string using the `split()`, `reverse()`, and `join()` methods. It then checks if the reversed string is equal to the original string and returns true if they are equal and false otherwise.
628+
629+
In the example usage, we pass the strings `racecar` and `hello` to the isPalindrome function. The function returns true for the first string because it is a palindrome (i.e., the reversed string is equal to the original string), and false for the second string.
630+
631+
</details>
632+
633+
<details>
634+
<summary>
635+
<h3>22. Write a function in JavaScript that finds the second highest number in an array of numbers.
636+
</h3>
637+
</summary>
638+
Answer:
639+
640+
```js
641+
function findSecondHighest(arr) {
642+
const sortedArr = arr.sort((a, b) => b - a);
643+
return sortedArr[1];
644+
}
645+
646+
// Example usage:
647+
const numbers = [10, 5, 20, 15, 8];
648+
console.log(findSecondHighest(numbers)); // Output: 15
649+
```
650+
651+
The `findSecondHighest` function takes an array of numbers as its parameter and first sorts the array in descending order using the `sort()` method and a comparison function. It then returns the second element in the sorted array, which is the second highest number.
652+
653+
In the example usage, we pass the array `[10, 5, 20, 15, 8]` to the findSecondHighest function. The function returns `15`, which is the second highest number in the array.
654+
655+
</details>
656+
657+
<details>
658+
<summary>
659+
<h3>23. Write a function in JavaScript that removes duplicates from an array.
660+
</h3>
661+
</summary>
662+
Answer:
663+
664+
```js
665+
function removeDuplicates(arr) {
666+
return [...new Set(arr)];
667+
}
668+
669+
// Example usage:
670+
const numbers = [1, 2, 3, 2, 1, 4, 5, 4];
671+
console.log(removeDuplicates(numbers)); // Output: [1, 2, 3, 4, 5]
672+
```
673+
674+
The `removeDuplicates` function takes an array as its parameter and first creates a Set object from the array using the `Set()` constructor. A Set is a collection of unique values, so all duplicates are automatically removed. We then use the spread syntax `(...)` to convert the Set back to an array.
675+
676+
In the example usage, we pass the array `[1, 2, 3, 2, 1, 4, 5, 4]` to the removeDuplicates function. The function returns a new array with duplicates removed, which is `[1, 2, 3, 4, 5]`.
677+
678+
</details>
679+
680+
<details>
681+
<summary>
682+
<h3>24. Write a function in JavaScript that returns the sum of two numbers without using the + operator.</h3>
683+
</summary>
684+
Answer:
685+
686+
```jsx
687+
function add(a, b) {
688+
while (b !== 0) {
689+
let carry = a & b;
690+
a = a ^ b;
691+
b = carry << 1;
692+
}
693+
return a;
694+
}
695+
696+
// Example usage:
697+
console.log(add(5, 7)); // Output: 12
698+
```
699+
700+
The add function takes two numbers a and b as its parameters and uses a bitwise approach to add them. It repeatedly performs a bitwise AND operation between a and b to get the carry bits and performs a bitwise XOR operation between a and b to get the sum bits. It then shifts the carry bits one position to the left and assigns the result to b. This process continues until b is equal to 0. The final value of a is the sum of the two numbers.
701+
702+
In the example usage, we pass the numbers 5 and 7 to the add function. The function returns 12, which is the sum of the two numbers.
703+
704+
</details>
705+
706+
<details>
707+
<summary>
708+
<h3>25. Write a function in JavaScript that checks if a given string is a valid IPv4 address.</h3>
709+
</summary>
710+
Answer:
711+
712+
```jsx
713+
function isValidIPv4(str) {
714+
const octets = str.split(".");
715+
if (octets.length !== 4) return false;
716+
for (let i = 0; i < octets.length; i++) {
717+
const octet = octets[i];
718+
if (isNaN(octet) || octet < 0 || octet > 255) return false;
719+
if (octet.length > 1 && octet[0] === "0") return false;
720+
}
721+
return true;
722+
}
723+
724+
// Example usage:
725+
console.log(isValidIPv4("192.168.0.1")); // Output: true
726+
console.log(isValidIPv4("256.0.0.0")); // Output: false
727+
```
728+
729+
The `isValidIPv4` function takes a string as its parameter and checks if it is a valid `IPv4` address.
730+
731+
An IPv4 address consists of four octets separated by periods, with each octet being a number between 0 and 255.
732+
733+
The function first splits the string into an array of octets using the split() method.
734+
735+
It then checks if the array has exactly four elements and if each element is a number between 0 and 255.
736+
737+
It also checks if each octet does not start with a 0 unless it is a single-digit octet.
738+
739+
If any of these conditions are not met, the function returns false. Otherwise, it returns true.
740+
741+
In the example usage, we pass the strings '192.168.0.1' and '256.0.0.0' to the isValidIPv4 function. The function returns true for the first string because it is a valid IPv4 address and false for the second string because it is not a valid IPv4 address.
742+
743+
</details>
744+
745+
<details>
746+
<summary>
747+
<h3>26. Write a function in JavaScript that converts a given string to title case.</h3>
748+
</summary>
749+
Answer:
750+
751+
```jsx
752+
function toTitleCase(str) {
753+
return str.replace(/\b\w+/g, function (word) {
754+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
755+
});
756+
}
757+
758+
// Example usage:
759+
console.log(toTitleCase("the quick brown fox")); // Output: 'The Quick Brown Fox'
760+
```
761+
762+
The `toTitleCase` function takes a string as its parameter and converts it to title case. Title case is a style of capitalization where the first letter of each word is capitalized, and the rest of the letters are in lowercase.
763+
764+
The function uses a regular expression to match the first letter of each word in the string and then uses the `charAt()` and `slice()` methods to capitalize the first letter and convert the rest of the letters to lowercase. It returns the modified string.
765+
766+
In the example usage, we pass the string 'the quick brown fox' to the toTitleCase function. The function returns 'The Quick Brown Fox', which is the string converted to title case.
767+
768+
</details>
769+
770+
<details>
771+
<summary>
772+
<h3>27. Write a function in JavaScript that generates a random hexadecimal color code.</h3>
773+
</summary>
774+
Answer:
775+
776+
```jsx
777+
function getRandomColor() {
778+
const hexChars = "0123456789ABCDEF";
779+
let color = "#";
780+
for (let i = 0; i < 6; i++) {
781+
color += hexChars[Math.floor(Math.random() * 16)];
782+
}
783+
return color;
784+
}
785+
786+
// Example usage:
787+
console.log(getRandomColor()); // Output: e.g. '#3D5A89'
788+
```
789+
790+
The `getRandomColor` function generates a random hexadecimal color code. A hexadecimal color code is a six-digit code that represents a color by specifying the amount of red, green, and blue in it.
791+
792+
Each digit of the code can be any of the sixteen hexadecimal characters (0 to 9 and A to F).
793+
794+
The function generates a random color code by selecting six random hexadecimal characters from the hexChars string using the Math.random() function and concatenating them together with a # symbol.
795+
796+
It then returns the generated color code.
797+
798+
In the example usage, we call the getRandomColor function, which returns a randomly generated color code. The output can vary each time the function is called, but it should be a valid hexadecimal color code.
799+
800+
</details>
801+
802+
<details>
803+
<summary>
804+
<h3>28. Guess the output of the following code:</h3>
805+
806+
```jsx
807+
let x = 5;
808+
{
809+
let x = 10;
810+
console.log(x);
811+
}
812+
console.log(x);
813+
```
814+
815+
</summary>
816+
817+
The output of the code above is:
818+
819+
Answer:
820+
821+
```bash
822+
10
823+
5
824+
```
825+
826+
This is because of the block scoping behavior of the let keyword in ES6.
827+
828+
When we declare a variable with `let` inside a block (in this case, the block is defined by the curly braces), the variable is only accessible inside that block and its sub-blocks.
829+
830+
In the code above, we define a variable `x` outside the block with the value of `5`. Then we define another variable x inside the block with the value of 10.
831+
832+
The first `console.log()` statement inside the block will print 10, because x refers to the variable defined inside the block. The second console.log() statement outside the block will print 5, because it refers to the variable defined outside the block.
833+
834+
</details>
835+
836+
<details>
837+
<summary>
838+
<h3>29. Guess the output of the following code:</h3>
839+
840+
```jsx
841+
const obj = { a: 1, b: 2, c: 3 };
842+
const { a, b } = obj;
843+
console.log(a, b);
844+
```
845+
846+
</summary>
847+
848+
The output of the code above is:
849+
850+
Answer:
851+
852+
```bash
853+
1 2
854+
```
855+
856+
This is because of the object destructuring syntax introduced in ES6.
857+
858+
We declare a constant variable obj with an object containing three properties. Then we use object destructuring to extract the properties a and b from the object and assign them to separate variables with the same names.
859+
860+
The console.log() statement then prints the values of the a and b variables, which are 1 and 2 respectively.
861+
862+
</details>
863+
864+
<details>
865+
<summary>
866+
<h3>30. Guess the output of the following code:</h3>
867+
868+
```jsx
869+
const arr1 = [1, 2, 3];
870+
const arr2 = [4, 5, 6];
871+
const arr3 = [...arr1, ...arr2];
872+
console.log(arr3);
873+
```
874+
875+
</summary>
876+
877+
The output of the code above is:
878+
879+
Answer:
880+
881+
```bash
882+
[1, 2, 3, 4, 5, 6]
883+
```
884+
885+
This is because of the spread syntax `(...)` introduced in ES6.
886+
887+
The `...` operator can be used to spread the elements of an array or the properties of an object into a new array or object.
888+
889+
In the code above, we define two arrays arr1 and arr2. Then we create a new array arr3 by spreading the elements of arr1 and arr2 into it using the spread syntax.
890+
891+
The console.log() statement then prints the contents of arr3, which are the elements of arr1 followed by the elements of arr2.
892+
893+
</details>

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /