0

I have an array of the alphabet and 2 sets of values, Im trying to check if a value is between the 2 given values then print out the letter. My code:

var alphabet_num = {
"alphabet": ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"],
"start": ["0", "150", "300", "450", "600", "750", "900", "1050", "1200", "1350", "1500", "1650", "1800", "1950", "2100", "2250"],
"end": ["149", "299", "449", "599", "749", "899", "1049", "1199", "1349", "1499", "1649", "1799", "1949", "2099", "2249", "2399"]
};

var xValue = 76;
var result = alphabet_num.alphabet.map(
(k, i) => [
    k, alphabet_num.start[i], alphabet_num.end[i]

    if (xValue >= alphabet_num.start[i] && xValue <= alphabet_num.end[i]) {
        //a
    }
]
);
5
  • 1
    Would you please share the output that is expected? Commented Mar 26, 2022 at 6:08
  • Hi yeah I need the if statement part, so if xValue = 76 then it should check start number and end number then print out the letter A, xValue = 150 then its B etc etc Commented Mar 26, 2022 at 6:10
  • 1
    Mao would return you an array of element. Are you sure you want to use map. Commented Mar 26, 2022 at 6:20
  • You should use destructuring to obtain conditional array elements. Something like this: ...(cond ? ['a'] : []) . More about that here : 2ality.com/2017/04/conditional-literal-entries.html Commented Mar 26, 2022 at 6:21
  • I run this code and getting error .map( (k, i) => [.... so this should be .map( (k, i) => { .... and console result show as you want. Commented Mar 26, 2022 at 6:23

3 Answers 3

2

If you are looking for a specific value, you don't need the map function, you could solve it with find.

const result = alphabet_num.alphabet.find(
  (_letter, i) =>
    parseInt(alphabet_num.start[i]) <= xValue &&
    xValue <= parseInt(alphabet_num.end[i])
);
Sign up to request clarification or add additional context in comments.

Comments

1

<script>
var alphabet_num = {
"alphabet": ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"],
"start": ["0", "150", "300", "450", "600", "750", "900", "1050", "1200", "1350", "1500", "1650", "1800", "1950", "2100", "2250"],
"end": ["149", "299", "449", "599", "749", "899", "1049", "1199", "1349", "1499", "1649", "1799", "1949", "2099", "2249", "2399"]
};


var xValue = 350;

var result = alphabet_num.alphabet.map(
    function(k, i){
        if (xValue >= alphabet_num.start[i] && xValue <= alphabet_num.end[i]) {
            console.log(alphabet_num.alphabet[i]);
            return alphabet_num.alphabet[i];
        }else{
            return '0';
        }
    }

);
// with 350 the array result
// result = ['0', '0', 'C', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
console.log(result);

// user findIndex function to find indx
let index = result.findIndex(function (value){
    return value!='0';
});

// the index of 'C' letter in array result index =2
console.log(index);

// get result[2] so I get 'C' letter
console.log(result[index]);
</script>

1 Comment

Thanks for your answer, don't forget to include a short description, welcome to SO!
0

You can use this

    alphabet_numBetweenStartAndEnd = function (alphabet_num, xValue) {
    for (var i = 0; i < alphabet_num.start.length; i++) {
        if (xValue >= alphabet_num.start[i] && xValue <= alphabet_num.end[i]) {
            return alphabet_num.alphabet[i];
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.