I've been working on code to convert strings into dictionaries and arrays, and vice versa. While string to array and string to object conversions are successful, the reverse process is giving me trouble. I'm currently stuck and unsure of how to resolve the issue. The results of my testing have not been satisfactory.
function strToArray(string) {
let array = [];
for (var element of string) {
array.push(element);
}
return array;
};
function strToObj(string) {
let dict = {};
for (let i = 0; i < string.length; i++) {
let letter = string[i];
if (dict[letter]) {
dict[letter].push(i);
} else {
dict[letter] = [i];
}
}
return dict;
};
function arrayToString(array) {
let text = "";
for (var element of array) {
text = text + element;
}
return text;
};
function objToString(obj) {
let text = "";
for (var element in obj) {
let value = obj[element];
}
return text;
};
const ArrayHello = strToArray('hello');
const ObjHello = strToObj('hello');
const HelloArray = arrayToString(ArrayHello);
const HelloObj = objToString(ObjHello);
console.log(ArrayHello);
console.log(ObjHello);
console.log(HelloArray);
console.log(HelloObj);