Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions javascript_solution.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
const json = require('./tests/challenge.json');

const refineParameters = data => {
// Write your code here.
// Remember to call this function to return the formatted json
// with the json imported at the top of this file

// Run this file with `node javascript_solution.js` in your CLI to verify your answer

const isCharInt = (character) => {
return character >= '0' && character <= '9';
}

const isWordNum = (word) => {
let dotFound = false;
for(let i=0; i<word.length; i++){
if(!isCharInt(word[i])){
if(!dotFound && word[i] === "."){
dotFound = true;
} else if (!(i === 0 && word[i] === "-" && word.length !== 1)) {
return false;
}
}
}
return true;
}

const evaluateString = (s) => {
if(s === null || s === undefined){
return s;
} else if(s === true || s === "true"){
return true;
} else if(s === false || s === "false"){
return false;
} else if(isWordNum(s)){
return parseFloat(s);
} else {
return s;
}
}


const refineParameters = data => {
const keys = Object.keys(data);
keys.forEach((key) => {
const item = data[key];
if(Array.isArray(item)){
for (let i=0; i<item.length; i++){
if(typeof item[i] === 'object'){
refineParameters(item[i]);
} else {
item[i] = evaluateString(item[i]);
}
}
} else if(typeof item === 'object') {
refineParameters(item);
} else {
data[key] = evaluateString(item);
}
})
return data;
}

// Comment out the line below to console.log and call your function to debug
// console.log('formattedJson: ', refineParameters(json));
console.log('formattedJson: ', refineParameters(json));

// ----- Do not modify anything below this line (needed for test suite) ------
module.exports = refineParameters;
Loading