U
U
Urukhayy2015-05-09 07:45:29
Programming
Urukhayy, 2015-05-09 07:45:29

What is the best way to build and solve this problem?

It is necessary to optimally implement the principle of requesting values. Here is an example:
There is a stall that sells many kinds of fruits. Each fruit (for example, an apple, a pear) has its own variety. Each variety has its own price. When the customer selects one variety of apples, we must extract the price from the array to show him. But there is a catch. We also apply a different ID to each variety. Let one variety have ID = 1023, and let the other variety have ID one thousand less, let it be 24. Thus, it may be that one pear variety has ID = 3, and an apple variety has some ID = 4, then yes ID does not specify the order of varieties and types of fruit.
So what's the best way to sort these prices? Attention, it is desirable to solve the problem without using OOP.
Option 1:

// Создаём для каждого типа свой массив
var Apples = [150,300,450]; // цены на каждый сорт
var Oranges= [220,300,450]; // цены на каждый сорт
// и т.д. все типы фруктов

In this, we can introduce a service variable that will store the selected index in one of the arrays. But to set such a value, we need to build a condition through which we will find out which variety the user clicked on (for example, knowing the ID of the variety). Then for each variety and fruit, you will have to build a check structure for a list of certain IDs.
Option 2:
// создаём общий двумерный массив
var Array = [
[1000,250], // первое значение есть ID сорта (это может быть и яблоко, и груша и что угодно), второе - цена сорта
[1032,300], // ..
[2034,270] // ..
// ...
];

This method involves looping through the entire array to match the user-selected variety ID with the first value of each subarray.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2015-05-09
@Rsa97

sparse array . You can store, for example, in a tree - then the search will be fast, and the addition is slow, or in a list, then the addition will speed up, and the search will slow down.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question