3 Replies - 236 Views - Last Post: 11 April 2017 - 02:12 AM

#1 zyanedwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 17

Substitute an amount for every value of the range input

Posted 11 April 2017 - 12:34 AM

Hi everyone,

Is there any shortcut for this very long switch statement because for every slide the user does, each value must be substituted with the values inside the array
amts


Example: Value 1 = 10
Value 2 = 20 ... and so on

    <input type="range" min="1" max="12" value="1" />


    var amts = [10, 20, 30, 50, 80, 100, 150, 200, 250, 300, 400, 500]; // amounts available

    // On slider drag
    $('#amountSlider input[type=range]').on('input', function () {
        var val = parseInt($(this).val());

        switch (val) {
            case 1: console.log(amts[0]);
                break;
            case 2: console.log(amts[1]);
                break;
            case 3: console.log(amts[2]);
                break;
            case 4: console.log(amts[3]);
                break;
            case 5: console.log(amts[4]);
                break;
            case 6: console.log(amts[5]);
                break;
            case 7: console.log(amts[6]);
                break;
            case 8: console.log(amts[7]);
                break;
            case 9: console.log(amts[8]);
                break;
            case 10: console.log(amts[9]);
                break;
            case 11: console.log(amts[10]);
                break;
            default: console.log(amts[11]);
                break;
        }
    });


Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Substitute an amount for every value of the range input

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Substitute an amount for every value of the range input

Posted 11 April 2017 - 12:42 AM

console.log(amts[this.value - 1]);


Was This Post Helpful? 1
  • +
  • -

#3 zyanedwards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 17

Re: Substitute an amount for every value of the range input

Posted 11 April 2017 - 02:05 AM

thanks, I didn't expect that!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Substitute an amount for every value of the range input

Posted 11 April 2017 - 02:12 AM

mostly it's a matter of knowing when to use the built-in tools rather than your fovourite library, and some rationale thinking (pattern recognition).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1