Coder Social home page Coder Social logo

js-iterators-laundry-lab's Introduction

LaundryMap

Welcome to my LaundryMap! We offer the following services: iron, mend, clean. Unfortunately, we can only process individual items of clothing. We have not figured out how to process loads... ¯\(ツ)

Iron

Write a function iron that removes the wrinkles (capital letters) from a single piece of clothing.

function iron(clothing_item){
    // code in here
    // return ironed_clothing_item;
}

// DRIVER CODE
iron("grEEn shIrt"); //=> "green shirt"
Solution (Click Here)
function iron(clothing_item){
    return clothing_item.toLowerCase();
}

iron("grEEn shIrt"); //=> "green shirt"

Ironing Load - Sample Data

// input
var wrinkled_clothes = [
  "grEEn shIrt",
  "TubE sockS",
  "TIe Dye shIrt",
  "gray pants",
  "HAndKerChief",
  "whItE bLousE"
]

// expected output
var expected_ironed_clothes = [
  "green shirt",
  "tube socks",
  "tie dye shirt",
  "gray pants",
  "handkerchief",
  "white blouse"
]



Mend

Write a function mend that repairs/removes the holes ("/") in a single piece of clothing.

function mend(clothing_item){
    // code in here
    // return mended_clothing_item;
}

// DRIVER CODE
mend("tu/be socks"); //=> "tube socks"
Solution (Click Here)
function mend(clothing_item){
    return clothing_item.replace("/", "");
}

mend("tu/be socks"); //=> "tube socks"

Mending Load - Sample Data

// input
var torn_clothes = [
  "knit swe/ater",
  "tu/be socks",
  "blue je/ans",
  "whit/e blouse"
]

// expected output
var expected_mended_clothes = [
  "knit sweater",
  "tube socks",
  "blue jeans",
  "white blouse"
]



Clean

Write a function clean that removes the dirt ("*") from a single piece of clothing.

function clean(clothing_item){
    // code in here
    // return clean_clothing_item;
}

clean("*blue shirt"); //=> "blue shirt"
Solution (Click Here)
function clean(clothing_item){
    return clothing_item.replace("*", "");
}

clean("*blue shirt"); //=> "blue shirt"

Cleaning Load - Sample Data

var dirty_clothes = [
  "*blue shirt",
  "argyle s*ocks",
  "u*gly sweater",
  "brown plaid pa*nts",
  "paisl*ey dress shirt"
]

var expected_clean_clothes = [
  "blue shirt",
  "argyle socks",
  "ugly sweater",
  "brown plaid pants",
  "paisley dress shirt"
]



Core Challenge: Help us process loads!

Can you help the LaundryMap process an entire load of laundry, not just individual clothing items?

Given the inputs and expected output, specified above, how would you generate the expected output?

Hint: You're going to need to loop (or iterate!) over the clothes in each load of laundry!

Solutions

Using a painful manual approach (Click Here)
function iron(clothing_item){
    return clothing_item.toLowerCase();
}

var output = [];

output.push( iron(wrinkled_clothes[0]) );
output.push( iron(wrinkled_clothes[1]) );
output.push( iron(wrinkled_clothes[2]) );
output.push( iron(wrinkled_clothes[3]) );
output.push( iron(wrinkled_clothes[4]) );
output.push( iron(wrinkled_clothes[5]) );

console.log("ironed:", output);
Using a `for` loop (Click Here)
function iron(clothing_item){
    return clothing_item.toLowerCase();
}

var output = [];

for(var i=0; i<wrinkled_clothes.length; i++){
    output.push( iron(wrinkled_clothes[i]) );   
}

console.log("ironed:", output);
Using the `forEach` method (Click Here)
function iron(clothing_item){
    return clothing_item.toLowerCase();
}

var output = [];

wrinkled_clothes.forEach(function process_item(item){
    output.push( iron(item) );   
})

console.log("ironed:", output);
Using the `map` method (Click Here)
function iron(clothing_item){
    return clothing_item.toLowerCase();
}

var output = wrinkled_clothes.map(iron);

console.log("ironed:", output);

This works too:

var output = wrinkled_clothes.map(function iron(clothing_item){
    return clothing_item.toLowerCase();
});

console.log("ironed:", output);



Bonus: Processing Batches

The Boss is thrilled we can iron, mend, and clean an entire load of clothing. Let's take it a step further -- let's wrap it all up in a single mega doBatch function that uses iron, mend and clean internally!

// copy your `iron`, `mend`, `clean` functions up here (but do not modify!)

function doBatch(clothes){
    // your code here
}

doBatch(["pLe*A/tED SKirt", "f*AncY T/Ie"]);
//=> ["pleated skirt", "fancy tie"]
Solution (Click Here)
function doBatch(clothes) {
    return iron(mend(clean(clothes)));
}

doBatch(["pLe*A/tED SKirt", "f*AncY T/Ie"]);
//=> ["pleated skirt", "fancy tie"]

Super Bonus: Batches of batches

How would you process loads and loads of customer clothes, all at once? (and return/output them all at once too!)

Solution (Click Here)
function doBatches(loads_of_clothes) {
    return loads_of_clothes.map(function(clothes){
        return clothes.map(function(item){
            return iron(mend(clean(item)));
        });
    });
}

doBatches([
    ["pLe*A/tED SKirt", "f*AncY T/Ie"], // e.g. customer 1
    ["cumber*bund"],                    //      customer 2
    ["rEd tuX/edo", "red* soCks"]       //      customer 3
]);

//=> [
//     ["pLeated skirt", "fancy tie"],
//     ["cumberbund"],
//     ["red tuxedo", "red socks"]
//   ]



Stretch: Professional Sorts!

The Boss thinks it will make us look more professional if we sort the laundry alphabetically before returning it to the customer. Without modifying any of the functions you created above, how might you accomplish this goal?

Solution (Click Here)
output.sort(); // easy!

console.log(output);

Mega-Stretch: Numeric Sort

Now sort it by the length of the string, longest to shortest. Like a tidy pyramid of clothes!

Solution (Click Here)
output.sort(function(a,b){
    return a.length - b.length;
});

console.log(output);

js-iterators-laundry-lab's People

Contributors

nathanallen avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.