Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How To Return Values from one array when values from another array are selected by users in Javascript

I'm a new student of programming and I'm trying to figure out an assignment.

I have to write a function that returns values from one array when values from another array are selected by the user on a one-to-one basis.

I've tried the following among other things and it gives a reference error (Apple is not defined; but Apple is an element in the first array).

var valueFromSelect = Apple;

  for (var i = 0; i < fruits.length; i++){
    if(valueFromSelect == fruits[i]){
      console.log(prices[i]);
      break

In short, I want to return a price for each fruit that is selected. Here are the arrays:

var fruits = ["Apple", "Orange", "Banana", "Pear", "Pineapple", "Strawberry", "Blueberry"];
var prices = [1.50, 1.20, 1.05, 1.10, 3.00, 0.40, 0.10]

Thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Apple value must be a string if your array contain strings.

var valueFromSelect = "Apple";
var fruits = ["Banana", "Orange", "Apple"];

PS : You can simplify your code by using filter method (from Array prototype) like bellow :

var valueFromSelect = "Apple";
var fruits = ["Banana", "Orange", "Apple"];
var result = fruits.filter(fruit => valueFromSelect == fruit);

This will return you an Array containing each values who matched with items in your Array. so if result.length > 0 you can consider your parameter as 'matched'.

about 4 years ago · Juan Pablo Isaza Report

0

The error you are getting is because in order for JS to understand Apple is a string you have to surround it with " (double quotes) or ' (quotes).

The way it is right now, is telling JS to save into valueFromSelect the value stored in something called Apple which you have not defined yet.

Examples:

  • 'Apple'
  • "Apple"

This way it should work:

var valueFromSelect = 'Apple';

  for (var i = 0; i < fruits.length; i++){
    if(valueFromSelect == fruits[i]){
      console.log(prices[i]);
      break

Since you want a function that prints the price of a given fruit, you need to create a function that receives the selected fruit as a parameter:

function printFruitPrice(valueFromSelect) {
    for (var i = 0; i < fruits.length; i++){
        if(valueFromSelect == fruits[i]){
          console.log(prices[i]);
          break
        }
    }
}

Then use it like:

printFruitPrice('Apple');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!