Home > Back-end >  Is there any difference between bag= bag n - i 1 and bag = n - i 1
Is there any difference between bag= bag n - i 1 and bag = n - i 1

Time:02-13

Is there any difference between bag= bag n - i 1 and bag = n - i 1 bcs is shows differnt while running a program below:

var n = 5
for (i = 1; i <= n; i  ) {
  var bag = ""

  for (j = 1; j <= n - i   1; j  ) {
    bag  = n - j   1
  }
  console.log(bag)
}
// 2nd way
var n = 5; // 
for (let i = 1; i <= n; i  ) {
  var string = "";
  for (let j = 1; j <= n - i   1; j  ) {
    string = string   n - j   1;
  }
  console.log(string);
}

Replit link

CodePudding user response:

bag = bag n - i 1 and bag = n - i 1 are not the same.

  • In the former, bag is coerced into a number
  • In the latter, the result is being coered into a string

The coercision occurs at the subtraction operator:

console.log(''   1);
//=> '1'

console.log('' - 1);
//=> -1

CodePudding user response:

Yes, there is a difference. The difference is to do with the order that your operators are evaluated:

bag  = n - i   1

is the same as:

bag = bag   (n - i   1) // string concatenation as `bag` is a string and we're using ` `

Notice that n - i 1 is grouped (( )) because it is on the right-hand side of the =, so it is evaluated/calculated first before being concatenated with the string bag. Both of the above lines of code are different from your other alternative of:

bag = bag   n - i  1

Here there is no grouping, so the order in which the operators are evaluated is different (see operator precedence). The above is equivalent to:

bag = (bag   n) - (i   1) // (string) - (number)

... which you can hopefully see will give different results from the first two lines above, as you are now subtracting different quantities. Moreover, as you're using subtraction, your results will now be numbers.

CodePudding user response:

Why?

There is a difference because you are using the " " operator for concatenation and using a "-" in the same line, so JS is implicitly casting between strings and numbers for you.

In the example of bag = n - j 1, JS evaluates everything on the right side from left-to-right. Then that number is concatenated onto the end of the string named bag.

In the example of bag = bag n - j 1, JS again evaluates everything on the right side from left-to-right, but now we have a mixture of strings and numbers on the right, so you're seeing strange behavior resulting from the anti-pattern.

See here:

bag  = n - j   1       // => bag  = 5 - 1   1     => bag  = 5          => bag = "5"
bag = bag   n - j   1  // => bag = ""   5 - 1   1 => bag = "5" - 1   1 => bag = 5

On the next loop, with the second syntax, you'll be adding to a number instead of concatenating to a string, which is what is causing your problem.

Solution

To fix this, you can technically just keep the first syntax, or you can wrap the numbers in parentheses, like so: bag = bag (n - j 1), but I would recommend considering making your casts explicit, like bag = String(n - j 1) or bag = bag.concat(n - j 1) and prevent mixing data types to ensure bugs like these don't come up again.

CodePudding user response:

Statement x = x 1 and x = 1 are same. But in JavaScript: String (type) Integer (type) or Integer (type) String (type) will be a String. i.e.

var a1 = 'b'    1; // a1 is 'b1' which is string concatenating
console.log(a1);
var a2 = 'b'    1   2; // a2 is 'b12' which is string concatenating
console.log(a2);
var a3 =  1   2   'b' ; // a3 is '3b', here string concatenating after summing 1 and 2
console.log(a3);

  • Related