Soru

Zorluk: OrtaBasic Scripting Languages and Constructs

A technician is troubleshooting a custom JavaScript (`.js`) automation script used to audit network workstation licenses. The script includes the line `let activeLicenses = "45";` and later attempts to update the count using `activeLicenses = activeLicenses + 5;`. When executed, the script outputs `455` instead of `50`. Which of the following identifies the root cause of this output?

  1. The variable was declared as a string data type instead of an integer, causing the addition operator to concatenate the values.Cevap
  2. B
    The variable was initialized as an environment variable rather than a standard script variable, preventing arithmetic evaluation.
  3. C
    The script requires a conditional `FOR` loop construct to iterate over numerical variable assignments.
  4. D
    The script interpreter requires the `/a` command-line switch to enable mathematical calculation mode.

Cevap

The variable was declared as a string data type instead of an integer, causing the addition operator to concatenate the values.
Enclosing values in quotation marks defines them as string data types. When performing addition with a string operand in JavaScript, the interpreter converts numbers to text and concatenates them (e.g., '45' + 5 = '455'). To perform arithmetic addition, the variable must be defined as an integer without quotation marks.

Adım Adım Çözüm

1
Analyze the variable declaration in the script snippet.
The line `let activeLicenses = "45";` uses quotation marks, defining the value as a text string rather than a numerical integer.
Data types determine how values are stored and manipulated by the scripting engine.
2
Evaluate the operation `activeLicenses + 5` on a string data type.
Combining the string `"45"` with `5` using the `+` operator results in string concatenation (`"455"`), not numerical addition (`50`).
Scripting engines treat the `+` operator as a string concatenation tool when at least one operand is a string.
3
Determine the required resolution.
Removing quotation marks (`let activeLicenses = 45;`) sets the variable as an integer, allowing arithmetic addition.
Integer data types support direct arithmetic calculations.

Anahtar Kavram

Scripting Data Types (Strings vs. Integers)
Bu soruyu puanla