2

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 Answers 2

1

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)

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

2 Comments

Thank you so much! Any advice on getting the counter to increment as an function at the bottom repeats itself, such that kner changes its output as the count increases? I'm new to javascript and I think tis has something to do with ansynchronousness.
check the answer again i update it by increment the counter each time you call your function
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.