Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Can someone explain why I'm getting zeros in my missed_questions array?

Elliot

New Coder
Here's the javascript code I did,

JavaScript:
public class SamDriverExam {
    private char []exam_answer_key = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
    private char []student_answer;
    
    public SamDriverExam(char []exam_answers)
       {
        student_answer = exam_answers;
       }
    
        public int totalCorrect()
        {
            int number_of_correct_answers = 0;
            for (int i = 0; i < exam_answer_key.length; i++)
            {
                if (exam_answer_key[i] == student_answer[i])
                {
                    number_of_correct_answers++;
                }
            }
            return number_of_correct_answers;
        }
        
        public int totalIncorrect()
        {
            int number_of_incorrect_answers = 0;
            for (int i = 0; i < exam_answer_key.length; i++)
            {
                if (exam_answer_key[i] != student_answer[i])
                {
                    number_of_incorrect_answers++;
                }
            }
            return number_of_incorrect_answers;
        }
        
        public boolean passed()
        {
            int passingscore = 0;
            for (int i = 0; i < exam_answer_key.length; i++)
            {
                if (exam_answer_key[i] == student_answer[i])
                {
                    passingscore++;
                }
            }
            
            if (passingscore >= 15 || passingscore >=20)
            {
                return true;
            }
            return false;
        }
        
        public int[] questionsMissed()
        {
            int [] missed_questions = new int [exam_answer_key.length];
            int index = 0;
            for (int i = 0; i < student_answer.length; i++)
            {
                if (student_answer[i]!= exam_answer_key[i])
                {
                    missed_questions[index] = i+1;
                    index = index+1;
                }
            }
            return missed_questions;
            
        }
}

Here is the output from my code
rHhKq.png

Closed. This question needs debugging details. It is not currently accepting answers.


Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.

Edit questionDelete question
Here's the java code I did,

public class BillyDriverExam {
private char []exam_answer_key = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
private char []student_answer;

public BillyDriverExam(char []exam_answers)
{
student_answer = exam_answers;
}

public int totalCorrect()
{
int number_of_correct_answers = 0;
for (int i = 0; i < exam_answer_key.length; i++)
{
if (exam_answer_key == student_answer)
{
number_of_correct_answers++;
}
}
return number_of_correct_answers;
}

public int totalIncorrect()
{
int number_of_incorrect_answers = 0;
for (int i = 0; i < exam_answer_key.length; i++)
{
if (exam_answer_key != student_answer)
{
number_of_incorrect_answers++;
}
}
return number_of_incorrect_answers;
}

public boolean passed()
{
int passingscore = 0;
for (int i = 0; i < exam_answer_key.length; i++)
{
if (exam_answer_key == student_answer)
{
passingscore++;
}
}

if (passingscore >= 15 || passingscore >=20)
{
return true;
}
return false;
}

public int[] questionsMissed()
{
int [] missed_questions = new int [exam_answer_key.length];
int index = 0;
for (int i = 0; i < student_answer.length; i++)
{[![enter image description here](https://i.stack.imgur.com/rUKNC.png)](https://i.stack.imgur.com/rUKNC.png)
if (student_answer!= exam_answer_key)
{
missed_questions[index] = i+1;
index = index+1;
}
}
return missed_questions;

}

}


Here is my output from my code,

enter image description here

I was expecting the missed questions to have no zeros in the array and only numbers from the user's input.

Here is the other part of my code

JavaScript:
 int[] missedQuestions = null;
SamDriverExam exam = new SamDriverExam(answers);
 missedQuestions = exam.questionsMissed();
if (missedQuestions != null)
          {
             System.out.println("You missed the following questions:");
             for (int i = 0; i < missedQuestions.length; i++)
                System.out.print(missedQuestions[i] + " ");
            
          }

Can someone help me on this?
 
I ran your code and got nothing. Guess we're missing the HTML/CSS for this.
I put the "Here is the other part of my code" at the end, is that where you have it, or is it someplace in the middle? Maybe you should give us the exact JS you're using.
 
Back
Top Bottom