I can't quite figure out the best way to approach this. I have two jQuery objects that both are used to set cookies. I then parse the JSON and set a cookie that is either the users height or the users weight.
This works fine, but I feel like there is an extreme amount of redundant code here that could be cleaned up but for the life of me I'm blanking on how to best do this. Ideally I'd like to be able to somehow condense this and have a function that I can pass in parameters if I want to receive the height cookie or the weight cookie.
Possibly something along the lines of: userInfo(heightCookie)
or userInfo(weightCookie)
.
I'm a bit of a newbie, so code examples are ideal, please.
var height_cookie = {
name: 'user-height',
options: {
path: '/',
expires: 365
}
};
var weight_cookie = {
name: 'user-weight',
options: {
path: '/',
expires: 365
}
};
function userHeightCookie() {
var userData = $.parseJSON($.cookie(height_cookie.name));
return(userData);
};
function userWeightCookie() {
var userData = $.parseJSON($.cookie(weight_cookie.name));
return(userData);
};
function readHeightCookie(userInfo) {
$.cookie(height_cookie.name, JSON.stringify(userInfo), height_cookie.options);
};
function readWeightCookie(userInfo) {
$.cookie(weight_cookie.name, JSON.stringify(userInfo), weight_cookie.options);
};
var userInfo = readHeightCookie();
var userInfo2 = readWeightCookie();