JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can it uppercase the string first letter

JavaScript Quiz – can it uppercase the string first letter

Uppercase First Letter –

In javascript, UpperCase First Letter means capitalize the first letter. Actually, in javascript either you can upper case everything using String.toUpperCase() or lower case everything using String.toLowerCase().

But in this case, we need to convert only first letter of each sentence to upper case.

It’s some looks like –

javascript tutorial = Javascript Tutorial

String Split Method –

Javascript string split method is used to break the string into multiple pieces based on the separator pass to it. Then it returns array after splitting which contains a substring of the main string.

It’s syntax looks like –

string.split(separator, limit)

For more detail about String Split Method click here.

Array Map Method –

In javascript array map method, is used to modify the element of array based on the condition passed to it. Map method introduce in ES6.

It’s syntax looks like –

array.map(function(currentValue, index, arr), thisValue)

For more detail about, Array Map Method click here.

Array Join Method –

JavaScript Array join method return an array as string. The element in the array when join together can be separated by separator pass to it. It can be a blank space, dash or anything.

It’s syntax looks like –

array.join(separator)

For more detail about, Array Join Method click here.

JavaScript Question

In this javascript quiz, let’s find out can it uppercase the string first letter as javascript only have upper case and lower case only.

First, we create a variable name using let keyword and assign a javascript string what is “js startup“.

let name = “js startup”;

Now, create another variable newString using let which store the result return from name.split().map().join();

let newString = name.split(…).map(…).join(…);

Finally, we are checking the value of newString using console log.

console.log(newString);

Any idea what, what will be the output.

Answer – “Js Startup

Click To Run Code

Explanation

Now, you know the answer is “Js Startup”. But why, what actually going on the all chaining. Let me explain.

First, we use a string split method using a separator blank space. Which convert it to array.

name.split(” “) = [“js”, “startup”];

In the next step, we use an array map method and inside it, we take the first character using charAt() method and convert it to the upper case.

And using splice, we remove first element from string. So it’s like.

el.charAt(0).toUpperCase() + el.slice(1) = “S” + “tartup”;

So, map will convert map to something like.

Array UpperCase First Letter –

name.spit(” “).map(el => { … }) = [“Js”, “Startup”];

And finally, we convert it back to a string using the array join method by passing the same blank space separator.

At last, That’s why we get an answer “Js Startup“.

Let me know in the comment section, any other alternative of it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – includes method can really remove duplicates with forEach
  2. JavaScript Quiz – can delete operator delete a global variable
  3. JavaScript Quiz – add new key in object after freeze is possible
  4. JS Quiz – new replaceAll method really replace all matching string

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link