function outputting function text rather than expected output

99 Views Asked by At

I've asked similar questions a few different ways, but here's the simplest version of it - I am trying to increment through a list of keyname values when a function occurs. However, when the function is called, all I am getting is the text of the function itself. Here's a snippet -

var knlist = {
    kn10:"2L1qvq6Tg6rMhEwNshr6dQ",
    kn11:"2N_Cl_Gl5fX8_TdLgHP3rQ",
    kn12:"2RbpjbhM3_EfzejfPgzwAw",
    kn13:"2rP8y_ub_alGrzAK_aZrEg",
    kn14:"2S8O9KBwxRlvtZX6kjyS0y",
    kn15:"2Ua5EnPVDwd7LGq6UbT2bQ",
    kn16:"3_17fNbyu2Yw8ozPx8BmkA",
    kn17:"3LB0GSXXVadBlCMhSth3IA",
    kn18:"48JvNwKSgvnWT8nqzWtE3Q",
    kn19:"4CP5JE_mlMMzjvDMMgXncg",
}

var count = 11

var knx = function knxer(){
    if (count === 11) {
    knx = "kn11";
    } else {
    knx = ("kn" + count);
    }};

var keyname = (knlist[knx]);

console.log (count)
console.log (knx)
console.log (keyname)

Console.log KNX is only giving me the text of the function knxer() itself rather than the expected values the function should return as the count increases.

Once this is solved, I'm going to be having another function increase the count within a different location - here's a full JSFiddle of where that is at. Once thats done I'm going to add an input for the login page so that username has a value that can be imputed the first time someone attempts the survey, and posts each completion over and over.

2

There are 2 best solutions below

2
On BEST ANSWER

The problem is that you assign knix to your function

var knlist = {
    kn10:"2L1qvq6Tg6rMhEwNshr6dQ",
    kn11:"2N_Cl_Gl5fX8_TdLgHP3rQ",
    kn12:"2RbpjbhM3_EfzejfPgzwAw",
    kn13:"2rP8y_ub_alGrzAK_aZrEg",
    kn14:"2S8O9KBwxRlvtZX6kjyS0y",
    kn15:"2Ua5EnPVDwd7LGq6UbT2bQ",
    kn16:"3_17fNbyu2Yw8ozPx8BmkA",
    kn17:"3LB0GSXXVadBlCMhSth3IA",
    kn18:"48JvNwKSgvnWT8nqzWtE3Q",
    kn19:"4CP5JE_mlMMzjvDMMgXncg",
}

var count = 11
var knx;

function knxer(){
    if (count === 11) {
      knx = "kn11";
    } else {
      knx = ("kn" + count++); // update the count each time it calls
    }};

knxer(); // call it 
var keyname = (knlist[knx]);

console.log (count)
console.log (knx)
console.log (keyname)

0
On

you have to call the function ,

you are just mentioning function name in console.log() ,

This will call the function and will return the value console.log(knx())

This will NOT call the function instead,It will return the function body console.log(knx)