question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Evaluate variable

See original GitHub issue

There is a possibility to evaluate any variable?

Example Javascript

var boxSize = 140
var boxSpace = 5

function calcBoxSize(size) {
    return boxSize * size + boxSpace * ((size - 1) * 2);
}

for(var i = -1; ++i < 10;) {
    eval('var boxSize' + i + ' = ' + calcBoxSize(i));
}

console.log(boxSize1); // 140
console.log(boxSize2); // 290

Suggestion in Sass

$boxSize: 140px
$boxSpace: 5px

@function calcBoxSize($size)
    @return $boxSize * $size + $boxSpace * (($size - 1) * 2)

@for $c from 1 through 10
    $calc: calcBoxSize($c)
    eval('$boxSize#{$c}: #{$calc}')

// Output
// $boxSize1: 140px
// $boxSize2: 290px
...

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Anahkiasencommented, Apr 1, 2014

eval is pretty much the one structure that is always frowned upon in all languages, Javascript, PHP, etc. Why should it be in Sass?

0reactions
lagdencommented, Apr 1, 2014

In the end…

Map solution

// Boxes vars
// -------------------------
$boxSize: 140px
$boxSpace: 5px
$boxMap: ()

// Boxes Helpers function
// -------------------------
@function calcBoxSize($size)
    @return $boxSize * $size + $boxSpace * ($size - 1) * 2

// Boxes
// -------------------------
@for $c from 1 through 10
    $boxMap: map-merge($boxMap, ($c: calcBoxSize($c))) !global

@debug map-get($boxMap, 1)
@debug map-get($boxMap, 2)
@debug map-get($boxMap, 3)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Evaluating expressions with two variables - Khan Academy
Evaluating expressions with two variables. CCSS.Math: ... We've done a few examples together where we were faced with 1 variable. Why not try...
Read more >
Evaluating an expression with one variable - Khan Academy
Learn how to evaluate an expression with variables using a technique called substitution (or "plugging in"). Created by Sal Khan and Monterey Institute...
Read more >
Evaluating expressions with one variable - Khan Academy
A mixture of explanations, examples, and practice problems to have you evaluating expressions with one variable in no time!
Read more >
How To Evaluate Expressions With Variables Using Order of ...
This Pre-Algebra video tutorial explains the process of evaluating expressions with variables, fractions, and exponents.
Read more >
Evaluate Expressions with Variables | Find the Value of an ...
Welcome to Evaluating Expressions with Variables with Mr. J! Need help with how to evaluate expressions with variables ?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found