JenMorgan / js-learning

0 stars 0 forks source link

Write a JavaScript function to get a copy of the object where the keys have become the values and the values the keys #26

Open kartamyshev opened 4 years ago

JenMorgan commented 4 years ago
const student = {
    name : "David Rayy",
    sclass : "VI",
    rollno : 12 
  };

const newObj = {};

function changeKeysAndValues (from_obj, to_obj) {
    for (let key in from_obj) {
        let new_key = key;
        key = from_obj[key];
        from_obj[key] = new_key;
        to_obj[key] = from_obj[key];
        delete from_obj[key];
    }
    return;
}

function displayTheObj (obj) {
    let print = " ";
    for (let key in obj) {
        print +=`${key} : ${obj[key]}, `;
    };
    print = `[${print}]`;
    return print;
};

changeKeysAndValues(student, newObj);
alert(displayTheObj(newObj));
alert(displayTheObj(student));