WessCoby
1/5/2019 - 10:08 PM

omit_data.js

/*
    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 
*/