Skip to content

Instantly share code, notes, and snippets.

@wesscoby
Last active April 13, 2019 19:46
Show Gist options
  • Save wesscoby/01689808cd8e2a38d8fbfd90940ae43a to your computer and use it in GitHub Desktop.
Save wesscoby/01689808cd8e2a38d8fbfd90940ae43a to your computer and use it in GitHub Desktop.
/*
36 | mary | accra | trader
24 | judith | kumasi | banker
22 | kofi | accra | trader
* The string above is formatted as 'AGE | NAME | LOCATION | WORK and separated with the ENTER key
* Given the string above, write a snipet that can delete any of the options above and return the same format of the string
* So, should the snipet delete the location column =, then your output should be:
36 | mary | trader
24 | judith | banker
22 | kofi | trader
*/
let omit_data = given_string => {
let split_given_string_into_array = given_string.split('\n').map(array_item => array_item.split('|'));
let random_option_to_delete = Math.floor(Math.random() * 4);
return split_given_string_into_array.map(item => item.filter((sub_array_item, index_of_array_item) => index_of_array_item !== random_option_to_delete)
.join("|"))
.join("\n");
};
console.log(
omit_data("36 | mary | accra | trader \n24 | judith | kumasi | banker \n22 | kofi | accra | trader")
);
/*
Output omits the option at random. Hence, a series of runs will produce the following results:
36 | mary | trader
24 | judith | banker
22 | kofi | trader
mary | accra | trader
judith | kumasi | banker
kofi | accra | trader
36 | mary | accra
24 | judith | kumasi
22 | kofi | accra
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment