Conclusion - Is there a better way to do this?


#1


Conclusion: I just tried to incorporate everything that I've learned, and this is what I came up with.


I am wondering, if there is a more effective way of writing this? My code seems cumbersome and not very scale-able. Any advice to make this better would be great!


myCandies = ["Lemmon Sours", "Cinnamon sticks", "Sour Patch Kids", "Chocolate"]
var yourCandies = prompt("What kinda candies do you like?")
console.log("So I hear you like " + yourCandies + "...");
var count = 0
for(var i=0; i<myCandies.length; i++){
    if (myCandies[i]=== yourCandies){
        var ohYeah = ("I like " + myCandies[i] + " too! Wanna join my secret club?");
        count= count + 1;
    }else{
        var ohNo = ("I don't really like " + yourCandies + ". Good bye.");
    }
}
if (count > 0){
    console.log(ohYeah);
}else{
    console.log(ohNo);
}


#2

count = count + 1;
==                         (++ is much more reader friendly)
count++;

I would check if their candy is contained by your list, rather than iterating through, which takes longer.

myCandies = ["Lemmon Sours", "Cinnamon sticks", "Sour Patch Kids", "Chocolate"];
yourCandies = prompt("What kinda candies do you like?");
console.log("So I hear you like " + yourCandies + "...");
if (myCandies.join(" ").contains(yourCandies)) {
  console.log("I like " + yourCandies + " too! Wanna join my secret club?");
} else {
  console.log("I don't really like " + yourCandies + ". Good bye.");
}

#3

That is a really smart way to do that! Thank you! How long have you been programming? and what are your tips to keep getting better?


#4

Thanks! I love refactoring code, there's always ways to improve!! :slight_smile:

I started on Codecademy about a year ago, before that I had no experience.

I found a great site called CodeWars, which helps me practice and learn more.

You complete coding challenges (make by fellow programmers, or yourself, for others), and then compare your solution to others.

It really helps to see some of the advanced syntax that you can put to use! :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.