I would like to ask you about the use of arguments objects and conditional expressions in terms of the three terms.

Asked 1 years ago, Updated 1 years ago, 83 views

Figure 1. Figure 2.

Like Figure 1 If the selector text (e.g. -=-) does not come like a red square, I changed/added the source code as shown in Figure 2,
I wonder why you used the conditional expression.

[Question] 1) Does *l -1 mean the end of the array? (Example: [0,1,2] where l-1 is 2) (l is lowercase English letter "L")

2) What does i <l - 1 mean to be true?

3) So, what does "selector" mean if true or "false"?

arguments fast-frontend javascript

2022-09-21 22:54

2 Answers

It's written in JavaScript, ternary operator grammar. Let me answer the question.

The trigonometric operator is written in the form of (to do when the conditional expression is true: to do when the conditional expression is false). Therefore, the for statement in question is extended and becomes like this. Would that be helpful?

for (; i < l; i++) {
    result += arguments[i];
    if (i < l - 1) {
        result += separator;
    } } else {
        result += '';
    }
}


2022-09-21 22:54

Hello, Roh Hyun-woo ^ - ^

Yupto answered well, so I don't know if I need an answer... I'm going to answer because you're asking questions after listening to the lecture.

First, the STEP 1 code is as follows: Script code where the first transfer factor value is inserted between the transferred factors following the first transfer factor value ' -=-' during the myConcat() function call.

function myConcat(separator) {
  var result = '';
  var i = 1;
  var l = arguments.length;
  for ( ; i < l; i++ ) {
    result += arguments[i] + separator;
  }
  return result;
}

myConcat(' -=- ', 'html', 'css', 'javascript', 'php', 'mySQL');

The code execution results are as follows: The problem is that you don't want to print the separator that was printed at the end here.

"html -=- css -=- javascript -=- php -=- mySQL -=- "

What is required to solve the problem is that the last iteration of the repeat statement should not have a separator set". Then we'll need a conditional statement inside the repeat statement to determine whether the cycle is last.

The last way to determine whether this is the case is to compare the total number of forwarding factors (arguments) minus 1 with the variable i in the for statement. (This part is ambiguous, right?))

Let's take a closer look at the STEP 2 syntax.

function myConcat(separator) {
  var result = '';
  var i = 1;
  var l = arguments.length;
  for ( ; i < l; i++ ) {
    // [Conditional statement]
    // CONDITION 1: If it's not the end of the repeat statement,
    if (i < l - 1) {
      result += arguments[i] + separator;
    } 
    // CONDITION 2: If it's the end of the repeat,
    else {
      Same as result +=arguments[i] + ''; // result +=arguments[i]
    }
  }
  return result;
}

myConcat(' -=- ', 'html', 'css', 'javascript', 'php', 'mySQL');

Here's the answer to the question.

arguments refers to the set of factors that the user passed during the function call. The arguments object will consist of 6 items since a total of 6 factors were passed during the call process.

myConcat(' -=- ', 'html', 'css', 'javascript', 'php', 'mySQL');

In other words, the variable = arguments.length syntax indicates that the l value is a number 6. The variable i value is set to start with the number 1, so the number of iterations will be repeated 5 from 1 to 5 depending on i <l. (Total number of iterations is 5)

In the conditional statement inside the iteration, the first condition is i <l - 1. Because l-1 is 5, it will be processed until the i value starting with 1 is less than the number 5. In other words, the total number of repeats is 4 times . 5Repeat 4 times, so All cases except the last iteration will be handled here.

if (i < l - 1) {
  // ...
}

In , the remaining else is processed as the remaining 5th iteration. In other words, The interval in which the last of the repetition statement is processed.

else {
  // ...
}

Can you wrap it up?

In the first condition processing of the if statement, ' -=-' is joined to the result character, but in the last condition processing, the '' empty string is joined. (If you put an empty character on the letter, the result is just that letter)

Simply put, html, css, javascript, php transfer factor 1 to 4 are followed by -=- and the last transferor is a blank when it is one. The final result is output as follows:

"html -=- css -=- javascript -=- php -=- mySQL"

The finish is STEP 3, which is a code simplification step. Replace the if~else conditional statement in STEP 2 consisting of multiple lines with a single line of three-term conditional expressions.

Shall we look at the code?

function myConcat(separator) {
  var result = '';
  var i = 1;
  var l = arguments.length;
  for ( ; i < l; i++ ) {
    // Simplify conditional statements using a three-term conditional expression
    // Conditions? If the condition is true: If the condition is false,
    result += arguments[i] + ( i < l - 1 ? separator : '' );
  }
  return result;
}

myConcat(' -=- ', 'html', 'css', 'javascript', 'php', 'mySQL');

I changed the conditional sentence, which was a little bit lengthy, to a concise conditional expression. Using a triad operator (?), :).

Let's compare the conditional statement in STEP2 with the conditional expression in STEP3.

Conditional statement below

if (i < l - 1) {
  result += arguments[i] + separator;
} } else {
  result += arguments[i] + '';
}

You can change to the condition expression as follows:

result += arguments[i] + ( i < l - 1 ? separator : '' )

The comparison process will help you determine which part of the conditional statement was imported and used for the conditional expression. The part corresponding to the condition is divided into phrases that are processed if the condition is true, and phrases that are processed if the condition is false. The basic syntax of the three-term conditional expression is as follows:

Condition? True: False

The results are the same as STEP 2. ^ ^ - ^

"html -=- css -=- javascript -=- php -=- mySQL"

It is not necessary to replace all codes with a three-term conditional expression. If the creator thinks it's necessary, change the conditional statement code, or use the conditional expression from the beginning. ^ - ^

If you've read the text and you still don't understand it, watch the Condition (3) Condition processing using operator expressions & logic operators (18:31) video over and over again. ^ - ^


2022-09-21 22:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.