Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Array keeps returning undefined

Options
  • 04-12-2014 8:20pm
    #1
    Registered Users Posts: 1,298 ✭✭✭


    //get user input
    var userInput = "This is a test";
    userInput = userInput.toLowerCase();

    userArray = userInput.split("");
    userArray = userArray.filter(function(n){return n != " "});
    userArray.sort();

    console.log(userArray);

    var result = [];
    result = result.sort(function(a,b){return a-b});


    for(i = 0; i < userArray.length; i++) {
    if(!result[userArray])
    result[userArray] = 0;
    ++result[userArray];
    result[userArray].push
    }



    console.log(result);



    var container = document.getElementById('container');
    var table = document.createElement("table");




    container.appendChild(table)





    trying to get the frequency of characters and print it to a table. This keeps telling me that either an array or an object is undefined. Anyone got any ideas on how to go about this?


Comments

  • Registered Users Posts: 553 ✭✭✭redman85


    // instead of an array store results in an object , {}
    var result = [];
    result = result.sort(function(a,b){return a-b});


    for(i = 0; i < userArray.length; i++) {

    if(!result[userArray]) {
    result[userArray] = 0;
    }

    ++result[userArray];

    // dont need this anymore
    result[userArray].push
    }


    I would change results to be an object instead of an array, otherwise your code looks good.

    working example


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    The one problem that i'm having there is that result is still being sent back with a length of 0. All the data inside it is undefined.


  • Registered Users Posts: 553 ✭✭✭redman85


    The one problem that i'm having there is that result is still being sent back with a length of 0. All the data inside it is undefined.

    Did you check the link I provided all the data is stored in the object.

    if your trying to loop over the data you could use this [HTML]for(var key in result) {
    console.log(key + ' : ' + result[key]);
    }[/HTML]


  • Registered Users Posts: 4 Farawayhouse


    At first glance, should you not have a space here?

    userArray = userInput.split("");

    userArray = userInput.split(" ");

    You have to split something :)


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    var string;
    var result = [];
    var order = [];

    document.body.onload = setup;

    function setup(){
    document.getElementById('go').onclick = string;
    //gets the ball rolling by calling the string function which ggets a users input on click

    }

    function string(){
    //get the value inside the text field
    string = document.getElementById("userInput").value;
    //convert it to lower case
    string = string.toLowerCase();
    //remove any spaces
    string = string.replace(/\s+/g,"");
    //console.log(string);
    //call the rest of the functions
    countLetters();
    orderList();
    printTable();
    clearText();

    }

    function countLetters(){

    var idx = 0;
    var tempString = string;
    //console.log(tempString);

    while(idx < string.length){
    //count the letters in the string
    var count = 0;
    var idx2 = 0;
    var letters = string[idx];
    //count how many times the letter appears
    while(idx2 < tempString.length){
    if (letters === tempString[idx2]) {
    count = count + 1;
    tempString = tempString.substr(0,idx2) + tempString.substr(idx2 + 1);
    //console.log(tempString);
    }else{
    idx2++;
    }
    }
    if (count !== 0) {
    result[result.length] = [letters,count];
    }
    idx++
    }
    //console.log(result);
    }


    function orderList(){
    //creates variables to store the data
    var biggerLet, biggerCount,let, letCount, biggerLet;
    var lat = result;//creates a temp array based on results array.
    var idx=0;

    while(idx < lat.length){
    //moves highest letter up to first in the array
    biggerLet = lat[idx][0];
    biggerCount = lat[idx][1];
    biggerIdx = idx;
    var idx2 = idx+1;
    while(idx2 < lat.length){
    //if a higher letter is encountered it becomes first in the array
    let = lat[idx2][0];
    letCount = lat[idx2][1];
    if (letCount > biggerCount) {
    biggerIdx = idx2;
    biggerLet = let;
    biggerCount = letCount;
    }
    if (lat.length === 1) {
    order[order.length] = [lat[0][0], lat[0][1]]
    idx = 10;
    }
    if (idx2 === (lat.length -1 )) {
    //if at the end of the array add values to the array
    order[order.length] = [biggerLet, biggerCount];
    lat.splice(biggerIdx,1)
    }
    idx2 ++;
    }
    if (lat.length===1){
    //final letter goes to the end of the array
    order[order.length]= [lat[0][0],lat[0][1]];
    idx=10;
    }
    console.log("RESULT", order);
    }

    }

    function printTable(){
    var table = document.getElementById("resulttable");//get the table
    var top = table.insertRow(0);//insert a header row
    top.insertCell(0).innerHTML ="LETTER";//insert the headings
    top.insertCell(1).innerHTML="COUNT";
    var idx =0;
    var let, count;//declare variables
    while(idx < order.length){//while less than the length of the ordered array
    let = (order[idx][0]);
    count = (order[idx][1]);
    var row = table.insertRow(idx+1);
    var cell1 = row.insertCell(0);//insert cells to the table
    var cell2 = row.insertCell(1);
    cell1.innerHTML = let;//this one = letters
    cell2.innerHTML = count;//this one = the count
    idx++
    }
    }

    function clearText(){
    document.getElementById("userInput").value = " ";
    }






    This was the end result to count and sort it.


  • Advertisement
  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    Just incase anyone needs to do this in future.


  • Registered Users Posts: 2,789 ✭✭✭mightyreds


    what was the problem in the end was it the split


Advertisement