002.  Solving 30-Days-Of-JavaScript challenge questions.

002. Solving 30-Days-Of-JavaScript challenge questions.

Table of contents

DAY 2.

Welcome to day 2 of the 30 -Days-Of-JavaScript challenge by Asabeneh. Let's take this challenge to the next level by providing solutions to the questions in the Asabeneh's challenge. You can go ahead and do challenge while using this series of articles as your reference.

Visit Day-1 for day one questions.

Remember: In programming there is always another way of solving questions. Don't limit yourself to this article series alone.

Exercise: Level 1.

1. Declare a variable named challenge and assign it to an initial value '30 Days Of JavaScript'.

Simple !

Solution:

1.PNG

2. Print the string on the browser console using console.log()

The console is a window object. We use the log function to 'print/log/display something on the console'.

2.2.PNG

2.4.PNG

3. Print the length of the string on the browser console using console.log()

As the name suggests, the string length property is used to return the length of a string. Arrays (which will solve later) also have this property.

Note that all white spaces and characters are counted as well. You can try adding spaces and check your results.

Solution:

3.PNG

3.1.PNG

4. Change all the string characters to capital letters using toUpperCase() method

solution:

image.png

5. Change all the string characters to lowercase letters using toLowerCase() method

solution:

image.png

6. Cut (slice) out the first word of the string using substr() or substring() method.

These two methods optionally have two parameters each.

NOTE: However the parameters take different arguments.

Substr() takes the start index as first argument and the length as second argument.

substing() takes the start index as the first argument and the last index (where to stop ) as the second argument.

I only passed one argument to cut the first word. By default ,the JavaScript engine will return the rest of the uncut string.

Solution:

image.png

7. Slice out the phrase 'Days Of JavaScript' from '30 Days Of JavaScript'.

There are many ways that we can solve this. Since I thought the author literally meant that I use the slice method, that's what I exactly did.

The slice method has two parameters : start index as the first parameter and the index where we want to stop (end) as the second parameter.

If the character(s) you want to eliminate are at the start or end part (a negative value to start at the end) of the string you can pass first argument only. In this example, I used both one and two arguments to return the same result.

Solution:

image.png

8. Check if the string contains a word 'Script' using includes() method

Let's check whether the string '30 Days Of JavaScript' contains the word ''Script'. The return is a boolean: either true or false.

As you can see in the solution, this method is case-sensitive.

Solution:

image.png

9. Split the string into an array using split() method

Here let us break the string characters using the split method. We will use an empty string as the separator (no space in between the quotes) . This method returns a new array without changing it.

Since I used an empty string, spaces were also taken in to account when splitting the string into an array.

Solution:

image.png

10. Split the string '30 Days Of JavaScript' at the space using split() method

Now will use the space as a separator.

Solution:

image.png

11. 'Facebook, Google, Microsoft, Apple, IBM, Oracle, Amazon' split the string at the comma and change it to an array.

Let us use the comma as the separator.

Solution:

image.png

12. Change '30 Days Of JavaScript' to '30 Days Of Python' using replace() method.

This method takes two arguments i.e. string.replace(searchValue, replaceValue).

Solution:

image.png

13. What is character at index 15 in '30 Days Of JavaScript' string? Use charAt() method.

Returns the character at the specified index.

Remember indices are zero-based. This method has one parameter.

Pass in 15 as the index

Solution:

image.png

14. Use indexOf to determine the position of the first occurrence of 'a' in '30 Days Of JavaScript'.

indexof returns the position of the first occurrence of a substring. It has one parameter (searchString) . If you don't provide the substring, the search begins at the beginning of the string

Solution:

image.png

15. Use lastIndexOf to determine the position of the last occurrence of 'a' in 30 Days Of JavaScript.

It is the opposite of indexof.

Returns the last occurrence of a substring in the string. If you don't provide the substring, the search begins at the end of the string. Has one parameter (searchString)

Solution:

image.png

16. Use indexOf to find the position of the first occurrence of the word 'because' in the following sentence:'You cannot end a sentence with because because because is a conjunction'

This method returns the index of the first character in the first occurrence of the word 'because'.

Solution:

image.png

17. Use lastIndexOf to find the position of the last occurrence of the word because in the following sentence:'You cannot end a sentence with because because because is a conjunction'

This method returns the index of the first character in the last occurrence of the word 'because'.

Solution:

image.png

18. Use search to find the position of the first occurrence of the word 'because' in the following sentence:'You cannot end a sentence with because because because is a conjunction'

This method returns the index of the first character in the first occurrence of the word 'because'.

Solution:

image.png

19. Use trim() to remove any trailing white space at the beginning and the end of a string.E.g ' 30 Days Of JavaScript '.

This method removes leading and trailing white spaces from a string

Solution:

image.png

20. Use startsWith() method with the string '30 Days Of JavaScript' and make the result true

Returns true if the sequence of elements of searchString is the same as the corresponding elements of the string at start position. Otherwise returns false.

Note: White spaces are take in to consideration.

Solution:

image.png

21. Use endsWith() method with the string '30 Days Of JavaScript' and make the result true

Returns true if the sequence of elements of searchString is the same as the corresponding elements of the string at end position . Otherwise returns false.

White spaces 😂

Solution:

image.png

22. Use match() method to find all the a’s in '30 Days Of JavaScript'

We enclose whatever we want to match or search with two forward slashes i.e. /abcd../ then we add the global flag g. The flag tells the JavaScript engine to search or match all the occurrences of 'a' otherwise it will only return the first occurrence.

We'll learn more about regular expressions (Regex) in future articles of this series.

Solution:

image.png

23. Use concat() and merge '30 Days of' and 'JavaScript' to a single string, '30 Days Of JavaScript'

With numbers , we add whereas with strings we concatenate. Simple!

Read more on data types. And concatenation.

Solution:

image.png

24. Use repeat() method to print '30 Days Of JavaScript' 2 times

The repeat method takes an argument of number of times you want to repeat the string or word.

If you pass in zero: 0 , an empty string will be returned.

Solution:

image.png

Exercise: Level 2.

1. Using console.log() print out the following statement:

The quote 'There is no exercise better for the heart than reaching down and lifting people up.' by John Holmes teaches us to help one another.

Solution(s):

I. Enclosing the text with double quotes.

image.png

image.png

II. Escaping the inner quotes with a backslash.

image.png

image.png

III. Using template literals or back ticks. ``

image.png

image.png

2. Using console.log() print out the following quote by Mother Teresa:

"Love is not patronizing and charity isn't about pity, it is about love. Charity and love are the same -- with charity you give love, so don't just give money but reach out your hand instead."

Solution:

image.png

image.png

3. Check if typeof '10' is exactly equal to 10. If not make it exactly equal.

Based on the question, we're supposed to make the typeof '10' equal to that of 10.

Note that we are checking if the typeof '10' is strictly equal to the typeof 10. We are not checking whether '10' is strictly equal to 10.

Solution(s):

I. using the inbuilt Number object.

image.png

II. Appending + sign

image.png

III. Using parseInt

Use parsefloat for a decimal number. Though it still works even for non-decimal numbers.

image.png

Note that we can also convert the typeof 10 to a string using toString() method

4. Check if parseFloat('9.8') is equal to 10 if not make it exactly equal with 10.

Let's use Math.round or Math.ceil. You can solve this in many different ways.

Solution:

image.png

5. Check if 'on' is found in both python and jargon

As you can see, we have many different ways to solve this as well.

Use the internet to explore more ways to solve this question.

Solution(s):

image.png

6. 'I hope this course is not full of jargon.' Check if 'jargon' is in the sentence.

We can use regex, we can also use includes method among other methods.

Solution:

image.png

7. Generate a random number between 0 and 100 inclusively.

We'll use Math.random to generate a number between 0 and 0.999999.... , then we'll multiply the result with 101 so as to include 100. Otherwise the maximum number possible will be 99: (0.9999.. * 100 = 99.99..) .Then use the Math.floor method to remove the floats.

This operation returns one random number within the range 0 - 100 (100 included)

Solution:

image.png

8. Generate a random number between 50 and 100 inclusively.

This one is a bit tricky😂. Let's do this🚀

First, well do Math.floor(Math.random() * 51) to get random numbers between 0 - 50 (50 included). Then we'll add 50 to the random number to make it fall between 50 - 100 range.

Example, if our random number is 0 then we add 50 to the random number the result is 50.

Example 2, if our random number is 50 then we add 50 to the random number the result is 100

Example 3, if our random number is 7 then we add 50 to the random number the result is 57. And so on.

Solution:

image.png

Find another way to solve this and leave a comment.

9. Generate a random number between 0 and 255 inclusively.

We'll multiply our random number with 256 so as to include 255 before we can floor it.

Solution:

image.png

10. Access the 'JavaScript' string characters using a random number.

Let's use the charAt method. We'll use Math.random to generate a random number, then we multiply the random number by the length of our string so as to access all the indices. We use the floor method to round down to a whole number. We will then pass in the random index as an argument to our charAt parameter.

Note that there is no need of subtracting 1 from the length of the string since we'll floor (round down). This will include the last index.

Check out this experiment keenly

image.png

Solution:

image.png

11. Use console.log() and escape characters to print the following pattern.

The pattern that we're creating. image.png

\t - used to tab horizontally

\n - means new line

Solution(s): image.png

In browser console:

image.png

12. Use substr to slice out the phrase 'because because because' from the following sentence: 'You cannot end a sentence with because because because is a conjunction'

string.substr( startIndex, length).

Solution:

image.png

Exercise: Level 3.

1. 'Love is the best thing in this world. Some found their love and some are still looking for their love.' Count the number of word 'love' in this sentence.

Solution:

image.png

2. Use match() to count the number of all because in the following sentence:'You cannot end a sentence with because because because is a conjunction'

We'll solve regex questions in the near future. Stay tuned.

Solution:

image.png

3. Clean the following text and find the most frequent word (hint, use replace and regular expressions).

image.png

let's clean up the sentence by removing all none numeric and none alphabets (both cases) using ^ symbol. We replace these characters with an empty string (no space between).

Create a function, split the word then iterate over the split string to find the most appearing word

I couldn't find a way to solve without using a function. Any solution? Comment.

Solution:

image.png

As you can see, 'I' and 'teaching' are the most appearing word. They both appear 3 times

We can eliminate I if we need a word with more than one letter, and find the next most appearing word. Which is 'teaching'

Solution:

image.png

Congratulations. Day 2 questions have been solved.

Next day-3

Twitter