JS - The comparison of two values in separate arrays is not functioning as expected

Hello everyone, I could really use some assistance with the following code snippet:

    
var questions =[
  ['what is the capital of France?', 'paris'],
  ['how many hours in a day?', 24],
  ['how many sides does a triangle have?', 3]
]

var answers = [];
var rightAnswers = [];
var wrongAnswers = [];


for(i = 0; i<questions.length; i+=1){  
  answers.push(prompt(questions[i][0]).toLowerCase());
  
  if(questions[i][1] === answers[i]){
     // rightAnswers.push(questions[i][0]) 
     console.log("Great job!");
  }else{
     console.log("Oops, not quite");
  }
  
}

Having trouble comparing two elements from two arrays - any insights would be greatly appreciated! Thanks!

Answer №1

The prompt function is designed to return strings, but in this case, your answers are numbers. To fix this issue, simply switch from using the strict comparison operator === to the loose comparison operator ==.

    
var questions =[
  ['how many states?', 1],
  ['how many continents?', 2],
  ['how many legs?', 3]
]

var answers = [];
var rightAnswers = [];
var wrongAnswers = [];


for(i = 0; i<questions.length; i+=1){  
  answers.push(prompt(questions[i][0]).toLowerCase());
  
  if(questions[i][1] == answers[i]){
     // rightAnswers.push(questions[i][0]) 
     console.log("success!");
  }else{
     console.log("bummer");
  }
  
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain the currently selected HTML element using tinyMCE

Within my editor, I have the ability to choose text and show it using the code below: alert(tinyMCE.activeEditor.selection.getContent({format : "html"})); The problem is that this function only returns text and not HtmlElement. This means I am unable to ...

Integrating array elements into the keys and values of an object

Given the array below: const Array = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress'] How can I transform it into an object like this? const Object = { Michael: student, John: cop, Juli ...

I am in need of assistance in developing an image slider using react.js. Can you help

let projects = [{id: 1, img: "https://scontent-lax3-2.xx.fbcdn.net/v/t1.0-9/117405206_1535037326696077_4967665142447981078_o.png?_nc_cat=111&ccb=2&_nc_sid=730e14&_nc_ohc=XlNXGJF47E0AX8lB1fk&_nc_ht=scontent-lax3-2.xx&a ...

How to Retrieve Values from Dynamic Arrays in JavaScript Without Specifying Keys

var data = [ { 'Specials 5': 2192, 'dates': '2021-06-14' }, { 'Specials 8': 767, 'dates': '2021-06-16' }, { 'Specials 13': 2264,'dates': '2021-06-18' }, ] ...

The React material-table only updates and rerenders the table when the data is updated twice

Currently, I am utilizing a tool called material-table (check it out here: https://material-table.com/#/) which has been developed using React. The issue I am facing is that the data passed as a prop to material-table doesn't seem to update correctly ...

Is there a way to access a component's props within the getServerSideProps() method?

Is there a way to pass parameters to a React component and then access the values of those parameters within the getServerSideProps function of the component? I am utilizing the next framework in React JS. <Menu name={menuName} /> In this example, ...

Transferring an array to PHP using AJAX

In this coding scenario, I initialize my array: let myArray = []; Within a loop, elements are added to the array as follows: myArray.push($(this).data('id')); // Example: [1, 2, 3, 4] Subsequently, I transmit this data to PHP using AJAX throu ...

The FormData function is unable to retrieve the input fields of a form

My Unique Code <!DOCTYPE html> <html> <body> <div id="custom-form-sample"> <form enctype="multipart/form-data"> <input type="text" name="custom-text" /> <input type="radio" name="custom-radio" ...

How can I transform a collection of objects into an array using Javascript?

What would be the best way to convert this content into an array? https://i.sstatic.net/H6Fo9.png I have this: ...

Troubleshooting Navigation Bar Toggle Button Issue in Bootstrap 5

Currently, I am in the process of working on a web project that requires the implementation of a responsive sidebar. This sidebar should be toggleable using a button located in the navigation bar. My choice for the layout is Bootstrap, and I have come acr ...

Learn how to dynamically enable or disable the add and remove buttons in the PrimeNG PickList using Angular 2

I'm currently learning Angular 2 and I'm working on creating a dual list box using PrimeNG's pickList component (https://www.primefaces.org/primeng/#/picklist). Within the pickList, I have table data with 3 columns, along with ADD and REMO ...

Getting the width of children components in Reactjs can be achieved by using the

Check out this sample from the web: https://codesandbox.io/s/jovial-resonance-5mjq0 const Parent = styled.div` display: flex; width: 100%; background: yellow; justify-content: space-between; `; const Children = styled.div` display: flex; back ...

The Ajax request functions flawlessly on Mozilla but encounters issues on Chrome, causing confusion as it occasionally works

I am using a PHP file with a class and function to store data in the database, accessed via AJAX. While everything works smoothly in Mozilla, Chrome seems to be causing some issues. Strangely, sometimes it works fine, but other times it fails for no appare ...

Concentrate on what comes next

Within my JavaScript code, I have the following line: $('input')[$('input').index(this)+9].focus(); I intended for this line to focus on the next element. However, when it executes, I encounter the following error: Error: [$rootSc ...

Implement Babel with node.js 6.9

It puzzles me why some folks opt to use Babel for their Node.js projects. I personally utilize node 6.9 and have no issues writing ES6 code - from default arguments in functions to arrow functions, rest parameters, and spread syntax. Do you think Babel is ...

Rendering issue with component

I am encountered with a situation where one component is failing to render its sub-component. Surprisingly, there are no visible errors in the console. The data I'm expecting from the web call is also coming through without any issues. However, for so ...

Insert a dash between two spans, but ensure that it is only done when the left span consists of a single line

Apologies for the lackluster title, struggling to find a more suitable description for this particular issue. Within a container, there are 2 spans that contain varying content, making it difficult to rely solely on media queries. The length of text in th ...

Pause the ongoing execution in JavaScript by using clearInterval

In my Vue 2.0 app, I have a line of code that calls this.refreshState() every minute using the following interval: this.scheduler = setInterval(() => this.refreshState(), 60 * 1000) Later in the code, I need to ensure that the execution loop is halted ...

Highlighted Answer: Choosing between two options in the quiz

I am currently developing an employability quiz for my university project. Each question offers a choice between Yes and No answers. If the user selects the Yes answer, it will be highlighted in green and display the correct answer below. However, if the ...

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...