3

After an exhaustive search, I have yet to find a clear answer to address my problem. This may in part be due to incorrect or imprecise references to javascript terminology as object proiented programming is new to me.

var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];

var AlignDecimal = function(NumberArray){

    var numberStructure = [{
        value:0,
        toString:"",
        decimalIndex:-1,
        integer:"",
        integerLength:-1,
        mantissa:"",
        mantissaLength:-1,
        sign:""
    }];

    for (var i = 0; i < NumberArray.length; i++){
        numberStructure[i].value = NumberArray[i];
        println(numberStructure[i].value);
    }

};

AlignDecimal(numArray);

I'm hoping from the above that there's enough information to ascertain my ultimate programming objective.

Originally I tried:

numberStructure.value = NumberArray

This partially worked except that .value had become an array, rather than numberStructure[].value.

Then I tried:

numberStructure = NumberArray

Now numberStructure[] array recieved NumberArray[] but, obviously, numberStructure[].value was undefined.

Also tried:

for (var i = 0; i < NumberArray.length; i++){
    numberStructure[i].value.push(NumberArray[i]);
    println(numberStructure[i].value);
}

That didn't work either.

2
  • What is it that you want? Are you trying to get an array of numberStructure objects, one for each number in numArray? Commented Jan 15, 2015 at 14:15
  • @adam0101: Desrired result: numberStructure[0].value = NumberArray[0], numberStructure[1].value = NumberArray[1]... Commented Jan 15, 2015 at 14:19

3 Answers 3

2

Are you trying to get an array of numberStructure objects, one for each number in numArray? If so, this is one way to do it:

var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];

var AlignDecimal = function(NumberArray){

    function numberStructure(){
        this.value = 0;
        this.toString = "";
        this.decimalIndex = -1;
        this.integer = "";
        this.integerLength = -1;
        this.mantissa = "";
        this.mantissaLength = -1;
        this.sign = "";
    };

    var numberStructureArray = [];

    for (var i = 0; i < NumberArray.length; i++){
        var numStruct = new numberStructure();
        numStruct.value = NumberArray[i];
        numberStructureArray.push(numStruct);
        document.writeln(numberStructureArray[i].value + '<br/>');
    }

};

AlignDecimal(numArray);

You could also do it like this:

  
var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];

var AlignDecimal = function(NumberArray){

    var numberStructureArray = [];

    for (var i = 0; i < NumberArray.length; i++){    
        numberStructureArray.push({
            value:NumberArray[i],
            toString:"",
            decimalIndex:-1,
            integer:"",
            integerLength:-1,
            mantissa:"",
            mantissaLength:-1,
            sign:""
        });
        document.writeln(numberStructureArray[i].value + '<br/>');
    }
};

AlignDecimal(numArray);

Sign up to request clarification or add additional context in comments.

5 Comments

@redthumb, in regards to your edit, that wasn't a typo, both syntaxes will work.
@redthumb, I made it into a runnable code snippet so you can see how the other syntax works.
I'm using a scaled down js viewer/processor. It didn't like the function first. See my self answer below. If you could address the nbsp and monospace font issues, I'll clean it up. Thanks!
@redthumb, I edited your answer to wrap your output in <pre> tags which preserve the spaces and line breaks.
@redthumb, it doesn't have to be a non-breaking space does it? I changed it to just a regular space in your answer since we're using <pre> tags.
0

you need to try some thing like this:

     var numArray = [0.123456789, 31.415, 314.15, -314.15, 0.0, 0, 1, 10, 100];
    var numberStructureObj=[];
     var AlignDecimal = function(NumberArray){
    
         var numStruct=[];
        	 for (var i = 0; i < NumberArray.length; i++){
        		   var numberStructure = {// create a new object every time then assign the value
        			         value:0,
        			         toString:"",
        			         decimalIndex:-1,
        			         integer:"",
        			         integerLength:-1,
        			         mantissa:"",
        			         mantissaLength:-1,
        			         sign:""
        			     };
    			     numberStructure.value=NumberArray[i];
        		 numberStructureObj.push(numberStructure);
        	    }
         
         console.log(numberStructureObj);// object with assigned values and rest of the fields.
        document.writeln(JSON.stringify(numberStructureObj));// see objects in string format
     };
    
     AlignDecimal(numArray);

4 Comments

That doesen't do what I want. This merely put the last value from the source array into numberStructure.value. This doesn't expand the numberStructure[].value array.
what exactly you want.
Suchit Kumar: Desrired result: numberStructure[0].value = NumberArray[0], numberStructure[1].value = NumberArray[1]...
then you have to create multiple object of numberStructure for each i then assign the value in corresponding.
0

After hacking around with adam0101's example, I decided to give this "snipet" feature a try...

        var numArray = [0.123456789, 
                        31.415, 
                        314.15, 
                        -314.15, 
                        0.0, 
                        0, 
                        1, 
                        10, 
                        100,
                        10000,
                        -1000];

        var AlignDecimal = function(NumberArray, DecimalPlaces){

          var integerLengthMax = -1;
          var mantissaLengthMax = -1;
          
          var numberStructure = function(){
            this.value = 0;
            this.valueAbs = 0;
            this.toString = "";
            this.decimalIndex = -1;
            this.integer = "";
            this.integerLength = -1;
            this.mantissa = "";
            this.mantissaLength = -1;
            this.sign = "";
            this.formattedResult = "";
          };// end numberStructure

            var numberStructureArray = [];

            for (var i = 0; i < NumberArray.length; i++){
                
              var numStruct = new numberStructure();
              
              if(NumberArray[i] < 0){

                numStruct.sign = "-";

              }else if(NumberArray[i] > 0){

                numStruct.sign = " "; // make optional for +

              }else{

                numStruct.sign = " ";

              }// end if(NumberArray[i] <> 0 ...
              
              var power = Math.pow(10, DecimalPlaces);
              
              var rounded = (Math.round(NumberArray[i]*power))/power;
              
              numStruct.value = rounded;
              numStruct.valueAbs = Math.abs(rounded);
              numStruct.toString = numStruct.valueAbs.toString();

              if(numStruct.toString.indexOf(".") < 1){

                numStruct.toString += ".0";

              }
              
              numStruct.decimalIndex = numStruct.toString.indexOf(".");

              var splitNum = numStruct.toString.split(".");

              numStruct.integer = numStruct.sign + splitNum[0];
              numStruct.integerLength = numStruct.integer.length;
              
              numStruct.mantissa = splitNum[1];
              numStruct.mantissaLength = numStruct.mantissa.length;
              
              numberStructureArray.push(numStruct);
              
            }// end for (var i = 0; i < NumberArray.length; i++)
          
            // scan array for max integer and mantissa lengths:
            for(var i = 0; i < numberStructureArray.length; i++){

              if(numberStructureArray[i].integerLength > integerLengthMax){

                integerLengthMax = numberStructureArray[i].integerLength;
            
              }

              if(numberStructureArray[i].mantissaLength > mantissaLengthMax){
                
                mantissaLengthMax = numberStructureArray[i].mantissaLength;
            
              }

            }// end for(var i = 0; i < numberStructureArray.length
        
            // format number to align decimals:
            document.write('<pre>');
            for(var i = 0; i < numberStructureArray.length; i++){

              // prepend spaces to integer component:
              while(numberStructureArray[i].integerLength < integerLengthMax){
            
                // 
                numberStructureArray[i].integer = " " + numberStructureArray[i].integer;
    		
                numberStructureArray[i].integerLength = numberStructureArray[i].integer.length;
          
              }// end while(numberStructureArray[i].integerLength < integerLengthMax

              // append "0" to mantissa component:
              while(numberStructureArray[i].mantissaLength < mantissaLengthMax){
            
                numberStructureArray[i].mantissa += "0";
    		
                numberStructureArray[i].mantissaLength = numberStructureArray[i].mantissa.length;
          
              }// end while(numberStructureArray[i].mantissaLength < mantissaLengthMax
          
    	      // concatenate formatted integer + decimal + mantissa:
              numberStructureArray[i].formattedResult = numberStructureArray[i].integer + "." + numberStructureArray[i].mantissa;
            
            document.writeln(numberStructureArray[i].formattedResult);
            
            }// end for(var i = 0; i < numberStructureArray.length

          document.write('</pre>');
          
        };// end var AlignDecimal = function

        AlignDecimal(numArray, 1);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.