patternjavascriptMinor
JavaScript string manipulation with certain logic in it
Viewed 0 times
javascriptwithlogicmanipulationcertainstring
Problem
How would I make this code look less ugly?
// we are looping over an array of images
// width is a parameter passed to the current function
//
// the resulting string looks like this: "30px 0"
// (it's a tool that returns coordinates for a CSS sprite)
// when iterating over the first item, we don't need
// to add "-" and "px" to X coordinate
result.push(
( i === 0 ? "0" : "-" + i * ( ( is2PixelRatio ? width / 2 : width ) + 10 ) + "px" ) + " 0"
);Solution
Minor upgrades, nice idea for the prepop result Raynos
result = ["0 0"]; // Outside of loop
// ...
if (is2PixelRatio) width /= 2;
width += 10; // [Insert magik number explanation here]
result.push( -(i * width) + "px 0" ); // Negative numbers will convert to "-30"Code Snippets
result = ["0 0"]; // Outside of loop
// ...
if (is2PixelRatio) width /= 2;
width += 10; // [Insert magik number explanation here]
result.push( -(i * width) + "px 0" ); // Negative numbers will convert to "-30"Context
StackExchange Code Review Q#3195, answer score: 5
Revisions (0)
No revisions yet.