Interview Question 8 | Javascript Pro | @codeDynasty-O95 #javascript #coding
This code snippet performs a reversal of words in a given sentence. Let's break it down step by step:
1. The variable `input` is initialized with the string "Welcome to this Coding Challenge".
2. The `revWords` function takes a word as input and reverses it. It initializes an empty string `result` and then iterates over the characters of the word in reverse order. For each character, it appends it to the `result` string using the `+=` operator. Finally, it returns the reversed word.
3. The `revSentence` function takes the `input` string as an argument. It first splits the input string into an array of words using the `split(" ")` method, which splits the string at each occurrence of a space character.
4. The `map` method is called on the `words` array, and for each word, the `revWords` function is applied to reverse it. This creates a new array called `reverseWords`, where each word is reversed.
5. The `join(" ")` method is called on the `reverseWords` array, which concatenates all the elements of the array into a single string, separated by a space character.
6. The resulting reversed sentence is stored in the `reverseWords` variable.
7. Finally, the reversed sentence is printed to the console using `console.log(revSentence(input))`.
When you run this code, the output will be: `"emocleW ot siht gnidahgloC"`, which is the original sentence with each word reversed.