Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use the input of a function to act like an existing variable

E.G. I've set some ARRAYS and I want to use the input (in my case PRODUCT) to act like the ARRAYS names

const coffee = [0.5, 0.4, 0.45]


and when I input PRODUCT to be COFFEE (lowercase given ofc :) )
and when I try the console.log(product[index]) where INDEX is successfully set by the code it prints out the letter positioned on that INDEX place

if INDEX is 0 then COFFEE gives C as a result
and I want (product[index]) to give me 0.5 because it should be read as coffee[0]

What I have tried:

function priceCalc (input) {
const coffee = [0.5, 0.4, 0.45]
const water = [0.8, 0.7, 0.7]
const beer = [1.2, 1.15, 1.1]
const sweets = [1.45, 1.3, 1.35]
const peanuts = [1.6, 1.5, 1.55]
var town = input[0]
var product = input[1]
var qty = input[2]
if (town == `Sofia`) {index = 0};
if (town == `Plovdiv`) {index = 1};
if (town == `Varna`) {index = 2};
price = input[1][index]
console.log(price)
}
priceCalc([`Sofia`, `coffee`, 4]);
JavaScript

Posted
Updated 12-Jun-22 10:39am

This code is very confusing, namely because you're passing an array into the method instead of giving the method proper parameters. You can first improve your code by adding parameters to the method:
function priceCalc(town, product, qty) {

There we go, this is much cleaner. Next, onto the issue you're having. For the second argument (product) you're passing in the string `coffee`. What this means is when you call product[index] you're getting a character at that index in the string, which clearly isn't what you want. Ultimately, there's two ways you can accomplish what you're after. First is to declare the products outside of the function, and then pass the arrays into the method like so:
const coffee = [0.5, 0.4, 0.45]
const water = [0.8, 0.7, 0.7]
const beer = [1.2, 1.15, 1.1]
const sweets = [1.45, 1.3, 1.35]
const peanuts = [1.6, 1.5, 1.55]

function priceCalc(town, product, qty) {
  ..
  price = product[index];
  console.log(price);
}

priceCalc('Sofia', coffee, 4);

The alternative, if you've truly got your heart set on actually passing in the string version, is to instead create an object with the product names, and then reference that object:
function priceCalc(town, product, qty) {
  const products = {
    'coffee': [0.5, 0.4, 0.45],
    'water': [0.8, 0.7, 0.7],
    'beer': [1.2, 1.15, 1.1],
    'sweets': [1.45, 1.3, 1.35],
    'peanuts': [1.6, 1.5, 1.55]
  };

  ..
  price = products[product][index];
  console.log(price);
}

priceCalc('Sofia', 'coffee', 4);

What the above is doing is taking that string coffee and then looking up the associated array in the products object with the same product name. Then, we access the index of the array to get the price value.
 
Share this answer
 
Dude. This is it. The bigger Array (If I can call it that way) made the whole difference. Not that it gave me what I wanted as an answer which will make JavaScript so bumpy over the road but still this made the code look so elegant. Here it is in it's final stage. And btw... the given answer in the tutorial was so ugly to look and work with :D
Thanks! This I'll remember


function priceCalc (town, product, qty) {
    const products = {
        'coffee': [0.5, 0.4, 0.45],
        'water': [0.8, 0.7, 0.7],
        'beer': [1.2, 1.15, 1.1],
        'sweets': [1.45, 1.3, 1.35],
        'peanuts': [1.6, 1.5, 1.55]
      };
if (town == `Sofia`) {index = 0};
if (town == `Plovdiv`) {index = 1};
if (town == `Varna`) {index = 2};
price = products[product][index]
totalPrice = price * qty
console.log(totalPrice)
}
priceCalc(`Varna`, `beer`, 4);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900