FSR - 060523- Quiz2 Question 7


Please check the question. I think the question is wrong.
@Hiral_Khakhariya @drishtinayak2828-TA @titiksha.chakraborty

1 Like


And Question no 6 also
@Hiral_Khakhariya @drishtinayak2828-TA @titiksha.chakraborty

1 Like

for question no. 6

The question is correct, but it’s a little confusing when you see that we can console the original variable mStr . That’s why option 1 is correct. We cannot store mStr.trim() in any variable, so it doesn’t work. Firstly, we can store it in one variable, and after that, we can console it.

3 Likes

@Sayantan Hi
As you can below :-
this is for question 7:-


You can see answer directly in online compiler only
The correct option is: “We T”.

The slice() method in JavaScript extracts a section of a string and returns it as a new string, without modifying the original string. It takes two parameters: the starting index and the ending index.

In the provided code, mStr.slice(6, 10) is called. This means that it will extract characters from index 6 up to, but not including, index 10. Therefore, it will extract the substring “We T”.

When console.log() is executed with mStr.slice(6, 10) as the argument, it will print “We T” to the console.
For question 6 you can see:-


The correct option is: " Hello World ".

The trim() method in JavaScript removes whitespace from both the beginning and the end of a string. However, in the provided code, the trim() method is called on the mStr variable, but the result is not assigned to any variable. Therefore, the original value of mStr remains unchanged.

When console.log(mStr) is executed, it will print the original value of mStr, which is " Hello World ".
So as i can see there is no issues in given quiz you can check further.

2 Likes

Here is the detailed explaination of the answer to the question no:06
The code will print the original value of the mStr variable, including leading and trailing spaces. The trim() function, when called on a string, removes any leading and trailing whitespace from that string, but it does not modify the original string itself. Instead, it returns a new string with the trimmed whitespace.

Here’s the modified code:

var mStr = "  Hello  World ";

var trimmedStr = mStr.trim();

console.log(mStr); // Output: "  Hello  World "
console.log(trimmedStr); // Output: "Hello  World"

In this code, the trim() function is called on the mStr variable, and the result is assigned to a new variable called trimmedStr. The console.log(mStr) statement will print the original value of mStr, including the leading and trailing spaces, whereas console.log(trimmedStr) will print the trimmed version of the string, without the leading and trailing spaces.

2 Likes

This is the interpreted answer to the question number:07

Certainly! Let’s delve into a detailed explanation of why the code mStr.slice(6, 10) would result in the substring " We T".

First, let’s consider the original string: “Mo Tu We Th Fr Sa Su”.

Each character in the string has an associated index starting from 0. Here is the breakdown:

Index:   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
Char:    M  o  ' ' T  u  ' ' W  e  ' ' T  h  ' ' F  r  ' ' S  a  ' ' S  u

Now, let’s focus on the slice() function and its behaviour. The "slice()` function allows you to extract a portion of a string by specifying the starting and ending indices. The starting index is inclusive, while the ending index is exclusive, meaning the character at the ending index will not be included in the extracted substring.

In this case, mStr.slice(6, 10) indicates that we want to extract a substring starting from index 6 and ending at index 10. However, the character at index 10 will not be included.

Using the index breakdown above, we can determine which characters fall within the specified indices:

Index:   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
Char:    M  o  ' ' T  u  ' ' W  e  ' ' T  h  ' ' F  r  ' ' S  a  ' ' S  u
                        ^  ^  ^  ^

The characters at indices 6, 7, 8, and 9 are " W", “e”, " ", and “T”, respectively.

Therefore, when you use mStr.slice(6, 10), it will extract the characters from index 6 up to, but not including, index 10. Consequently, the resulting substring is " We T".

3 Likes

@drishtinayak2828-TA @Hiral_Khakhariya @titiksha.chakraborty
Sorry Ma’am, actually at first I didn’t got the questions . My bad :smiling_face_with_tear:

1 Like

Quite alright, @Sayantan .

2 Likes

It’s alright child no problem it’s a normal way to learn new things so it’s totally alright.

1 Like