0

I don't want to type out every possible price combination but I've been searching for a long time and can't find a better way:

var selection = new Array (4);
selection[0] = new Array ('$210' 'etc', 'etc', 'etc', 'etc');
selection[1] = new Array ('Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole');
selection[2] = new Array ('Talk', 'Talk & Text', 'Talk, Text & Data');
selection[3] = new Array ('One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four Years', 'Four');



function selectPhone () {

    var yourPhone = prompt("What kind of Smartphone would you like: Solar: $100, Supernova: $200, Quasar: $300, Galaxy: $400, Blackhole: $500?");

    if (yourPhone == selection[1][0]) {
        console.log("You picked: " + yourPhone + "."), selectPlan ();
    } else {
        console.log("Error.");
    }
}



function selectPlan () {

    var yourPlan = prompt("What Plan Would You Like: Talk: $10, Talk & Text: $20 or Talk, Text & Data: $30?");

    if (yourPlan == selection[2][0]) {
        console.log("You picked: " + yourPlan + "."), selectTerm ();
    } else {
        console.log("Error.");
    }
}



function selectTerm () {

    var yourTerm = prompt("What Term Would You Like: One Year: $100, Two Years: $200, Three Years: $300 or Four Years: $400?");

    if (yourTerm == selection[3][0] || selection [3][1]) {
        console.log("You picked: " + selection[3][0] + ". \n Your total is: " + selection[0][0]);
    } else {
        console.log("Error.");
    }
}


selectPhone ();

I can't figure out how to program it so it can just pick up the selections made and convert them to numeric values and perform simple addition on them. I'm a beginner so please explain everything. THANKS A LOT!!

Markus Hallcyon
  • 351
  • 2
  • 5
  • 15
  • 2
    Object make this beautiful. Why don't you want to use a simple JSON-based structure for your array data? – Diodeus - James MacFarlane Sep 27 '14 at 18:21
  • I need to do it without them. Only if statements, functions, variables and arrays. – Markus Hallcyon Sep 27 '14 at 18:23
  • This is starting to sound like a Homework Helper. – Diodeus - James MacFarlane Sep 27 '14 at 18:23
  • ...is that not allowed on Holy Stack Overflow? – Markus Hallcyon Sep 27 '14 at 18:26
  • 1
    I don't care if this is a homework or not, doesn't matter to me. But is that why you can't use objects? – p e p Sep 27 '14 at 18:27
  • It's allowed but it may cause people to question the integrity of the person posting the question. – Diodeus - James MacFarlane Sep 27 '14 at 18:28
  • Because we haven't learned about them yet and the assignment doesn't call for them. Have to show I understand these fully. – Markus Hallcyon Sep 27 '14 at 18:28
  • @Diodeus - that is false. Markus, asking for help if you are stuck on a part of a homework is OK. That is very different from coming here and asking for a complete solution to a problem. I think in your case, this sounds like a legitimate question and it doesn't sound like you are trying to cheat your way to things. See: http://meta.stackexchange.com/questions/18242/what-is-the-policy-here-on-homework – p e p Sep 27 '14 at 18:30
  • Are you allowed to use arrays, though? It seems like you are - and they would be a great help, combined with a loop. (though technically arrays *are* objects) – Bergi Sep 27 '14 at 18:55
  • Yeah I can use arrays. Really I just can't figure out how to avoid typing out a massive about of different combinations. – Markus Hallcyon Sep 27 '14 at 19:04
  • I'm curious, are you being taught to put commas between function calls (ex. console.log('...'), selectTerm();)? – 76484 Sep 27 '14 at 19:34
  • @MarkusHallcyon: You should use a [loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) :-) Also put the questions in an array (best use an array literal), and store the answers into an array as well. – Bergi Sep 27 '14 at 19:40
  • @MarkusHallcyon: In your selectTerm function, I think you mean to use `if (yourTerm == selection[3][0] || yourTerm == selection[3][1])` - there is a big difference between that and what you have. – 76484 Sep 27 '14 at 20:11

2 Answers2

1

You could just use something like parseInt() where you need to convert to integers. Here's the documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

Garrett Kadillak
  • 1,026
  • 9
  • 18
  • I tried implementing that for awhile and it wouldn't work. But I guess I'll keep trying. Thanks. – Markus Hallcyon Sep 27 '14 at 18:25
  • It would just put the two numbers beside each other, not add them. – Markus Hallcyon Sep 27 '14 at 18:30
  • A `+` before the defining of a variable works the same as parseInt() but is shorter. So instead of `x = parseInt(prompt())`, it would be `x = +prompt();` – Brendan Sep 27 '14 at 19:11
  • No problem. Whoever made javascript decided to give the `+` sign 3 or 4 things it can do which I don't understand but it is convinient – Brendan Sep 27 '14 at 19:24
  • Tried that Brendan, it just keeps giving me the two numbers beside eachother. Example: console.log("You picked: " + selection[3][0] + ". \n Your total is: " + parseInt(selection[0][0]) + parseInt(selection[0][1])); (i made it so selection[0][0] = 100 & selection[0][1] = 29.) I think it's because that is for variables and you have to define a variable. I need this to work for my nested array elements. – Markus Hallcyon Sep 28 '14 at 02:28
-1

You do it like this:

var selection = new Array (4);
selection[0] = new Array ('$210', 'etc', 'etc', 'etc', 'etc');
selection[1] = new Array ('Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole');
selection[2] = new Array ('Talk', 'Talk & Text', 'Talk, Text & Data');
selection[3] = new Array ('One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four Years', 'Four');


function selectPhone (selectionArray) {
    var yourPhone = prompt("What kind of Smartphone would you like: Solar?");
    for( s1 in selectionArray ){
        for( s2 in selectionArray[s1] ) {
            if (yourPhone == selectionArray[s1][s2] ) {
                console.log("You picked: " + yourPhone + ".");
            } else {
                console.log("Error.");
            }
        }
    }
}

selectPhone(selection);

You do need to know about objects though. Also look into the for .. in function in JS - documentation will explain it better than me, but in short, you loop through properties of the parent array(with the for..in) - basically through a list of arrays. Than using the same idea, you loop through the each array in part. To give you a visual representation, you array looks like this:

var selection  = [//outer array
                    [ //inner array - property of outer array (first for..in) = s1
                       '$210', //property of inner array (second for..in) = s2                          
                       'etc'
                    ], 
                    [ 'Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole' ], 
                    [ 'Talk', 'Talk & Text', 'Talk, Text & Data' ],
                    [ 'One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four' ]
                ]
Radu Andrei
  • 1,075
  • 10
  • 19
  • 1
    [No, you wouldn't](http://stackoverflow.com/q/500504/1048572). Please use the proper loop. – Bergi Sep 27 '14 at 18:56
  • Btw, your `console.log('Error')` seems to be misplaced, it doesn't make much sense inside the inner loop. – Bergi Sep 27 '14 at 18:57
  • @Bergi The specific example that i worked with for his specific needs is perfectly valid (and better than array.length) - though i agree it's not suited for every case. The console.log is also placed where it needs to be. Check the code next time, before you comment. – Radu Andrei Sep 27 '14 at 19:05
  • I must confess I haven't looked at your code close enough. I did again, and only found more mistakes - I'd downvote a second time if I could. Try your code out next time, before you answer. Hint: it currently just logs `Error` 21 times regardless of the input – Bergi Sep 27 '14 at 19:09
  • @Bergi Ah, you are quite right. I had forgotten the object itself. You're still wrong about the iteration over the arrays issue though. – Radu Andrei Sep 27 '14 at 19:20
  • No, that is a big issue, and you should especially care about JavaScript learners like the OP. Only because it works doesn't mean it's valid, being so easy to break. Btw, you also forgot to make the enumeration variables local (`var`). – Bergi Sep 27 '14 at 19:29
  • @Bergi I didn't spend too much time on it as you can tell, since i also made mistakes when posting the answer. In my view, the OP should look further into the methods presented in the answers. Maybe i am wrong on that count and higher code quality is desirable. – Radu Andrei Sep 27 '14 at 19:35
  • Or maybe less detailed code is desired and the OP would figure out syntax details himself :-) – Bergi Sep 27 '14 at 19:47