Node Js Coding Questions and Answers | Interview Question

·

2 min read

This article provides Node js coding questions and answers to help you better understand the basics of Node.js development. Get the answers you need to master Node.js coding and take your development skills to the next level.

Reverse a string: Write a function to reverse a given string in Node.js.


function reverseString(str) {

return str.split('').reverse().join('');

}


Explanation: The reverseString function takes a string as input. It uses split('') to convert the string into an array of characters, then uses reverse() to reverse the order of the array, and finally uses join('') to convert the array back to a string with reversed characters.

Find the first non-repeated character: Write a function to find the first non-repeated character in a given string in Node.js.


function findFirstNonRepeatedChar(str) {

let charCount = {};

for (let char of str) {

charCount[char] = (charCount[char] || 0) + 1;

}

for (let char of str) {

if (charCount[char] === 1) {

return char;

}

}

return null;

}


Explanation: The findFirstNonRepeatedChar function uses an object charCount to keep track of the count of each character in the input string. It then iterates through the string twice. First, it counts the occurrences of each character and stores them in charCount. Second, it finds the first character with a count of 1 in charCount and returns it as the first non-repeated character.