Disable button after random choice is finished

I’m trying to disable the next button after all my question is finished also the back button if there are no questions to go back to
simple to have a disable button when it doesn’t make sense to have it working

function GetValue()
{
    let indexOfQuestions;  
    do {
      indexOfQuestions = Math.floor(Math.random() * questions.length);
    } while(selectedQuestions.includes(indexOfQuestions));
 
    var random = questions[indexOfQuestions];
    selectedQuestions.push(indexOfQuestions);
    currentQuestion = selectedQuestions.length - 1;
    console.log(selectedQuestions);
    //alert(random);
    showMessage(random);
}

function showMessage(message) {
    document.getElementById("message").innerHTML=message;
}

function goBack() {
    currentQuestion = Math.max(--currentQuestion, 0);
    var message = questions[selectedQuestions[currentQuestion]];
    showMessage(message);
  console.log({currentQuestion, message})
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

thank you so much , I’m still new and fresh to the programmer community so I really appreciate all the help I get :slight_smile:

So you have an array, questions, which contains all the possible questions (i assume). Is selectedQuestions also an array? Is the intent of it to be the subset of all questions, and to include just the questions for this user?

Without seeing the whole code, i might suggest that you consider this - you have the array of user questions, and you might want to use a global (or higher-level) variable to contain the index of the current question. If that index is zero, disable the ‘Last’ button. If that index is equal to the length of the users questions array-1, disable the ‘Next’ button. In every other case, enable the buttons.

If you’d like more clarification, we’ll likely need more detailed code. Either way, here to help.

1 Like