Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript mapping numbers to string

I problem is in javaScript I want a list of cities with each city associated with a range of temperature such as Manchester 10-20, London 21-30. When a user inputs a temperature it tells the associated city (user inputted 22 London will be outputted).

My initial idea was to have a String array of cities then map users input to the index of the city. So user inputs 22 it maps it to index 2 (London).

But inputting a city later on will be really hard.

My second idea was just a ton if else statements starting with the lowest band.

These are my two ideas, I was wondering what it the best way to solve this problem.

like image 372
Yusof Bandar Avatar asked Apr 25 '26 13:04

Yusof Bandar


1 Answers

Make a 3 dimension

array [City][low-temp][high-temp]

If a user gives input run all cities and compare if the input is

low-temp < input < high-temp

and return list of cities which meet the condition.

like image 94
Mischa Avatar answered Apr 27 '26 02:04

Mischa