24.2k views
5 votes
Create a string called alphabet containing 'abcdefghijklmnopqrstuvwxyz', then perform the following separate slice operations to obtain: The first half of the string using starting and ending indices. The first half of the string using only the ending index. The second half of the string using starting and ending indices. The second half of the string using only the starting index. Every second letter in the string starting with 'a'. The entire string in reverse. Every third letter of the string in reverse starting with 'z'.

User Yingted
by
6.0k points

1 Answer

2 votes

Answer:

There is no short answer.

Step-by-step explanation:

First let's create the string:

  • alphabetString = "abcdefghijklmnopqrstuvwxyz";

The first half of the string using slice method can be written as:

  • alphabetString.slice(0, 13);

The first half of the string using only the ending index can be written as:

  • alphabetString.slice(-13);

When we put - at the start of the index number, the counting begins at the last element with -1 and goes backwards.

The second half of the string can be written as:

  • alphabetString.slice(13,26);

The second half of the string using only the starting index can be written as:

  • alphabetString.slice(13);

To get the every second letter in the string, we need a for loop:

  • for( let x = 0; x < alphabetString.length(); x = x + 2){

alphabetString.slice(x);

}

To get the entire string in reverse, we can use the reverse method that is built-in:

  • alphabetString.reverse();

To get the every third letter of the string, we can again use a for loop:

  • for( let x = -1; x = -27; x = x - 3){

alphabetString.slice(x);

}

I hope this answer helps.

User Fabrik
by
5.8k points