# Convert Seconds to Compound Rosetta: Formatting is Wrong

Tell us what’s happening:
Hey, so this is middle of the night on All Hallows Eve when posting, so kinda woozy like getting kicked in the face by a gazelle, but … I think I’m missing something in the formatting when converting the proper results… Getting the right values passed to console input at right index j, but not passing final string to return value, finalString . even when passing conditional if statements to prevent weird additional console passing while not properly going thru the while loop for greedy algorithm…
Tried all that I know, debugging-wise to get it right, so I’d like someone to help me ‘set things right where they once went wrong’ … Thnx…

Your code so far

``````
function convertSeconds(sec) {
var durationSuffix = ["sec", "min", "hr", "d", "wk"];
var conversionRatio = [60,3600,86400, 604800];
/*sec to sec ,sec to min, min to hr,hr to d, d to wk; count them in seconds */
var totalSeconds = sec, currentTotal = 0;
var arrConversion = [];
for(var j = durationSuffix.length -1; j>= 0; j--) {
currentTotal = 0;

while(totalSeconds >= conversionRatio[j]){
currentTotal++;
totalSeconds -= conversionRatio[j];
console.log(`conversionRatio for jth is \${conversionRatio[j]}, and totalSeconds, after reduced conversion is \${totalSeconds}, and currentTotal is \${currentTotal}.\n`);
}
if(totalSeconds < conversionRatio[j]) {
console.log(`Before I append to arrConversion, I am appending \${(currentTotal > 0 && totalSeconds < conversionRatio[j]) ? currentTotal: totalSeconds}.\n`);
arrConversion.unshift((currentTotal > 0 && totalSeconds < conversionRatio[j]) ? currentTotal: totalSeconds);
}
}
var finalString = "";
for(var j = arrConverision.length -1; j >= 0; j--) {
finalString += `\${arrConversion[j]} \${durationSuffix[j]}, `;
}

console.log(`Before returning value, the corresponding final string would appear to be something like this: \${finalString}.\n`);
return finalString;
}

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36`.

Challenge: Convert seconds to compound duration

Link to the challenge:

I haven’t read your code, cuz i discovered this problem now (from your post) and it’s a sort of cash register like in the js cert, and your solution just looks ok to me
i think the problem is

you have “60” below “sec”, which is wrong.
try adding “1” as the first value to the conversionRatio, think it’ll fix it, also, add return commands

in the meantime (not all time, but yeah) I had a solution, pretty similar to yours, cuz it’s the same concept, so:

``````function convertSeconds(sec) {
var vals = [60, 3600, 86400, 604800];
var strs = ['min', 'hr', 'd', 'wk'];
var res = ""
for (let i = vals.length - 1; i >= 0; i--) {
var iterations = 0;
while (sec >= vals[i]) {
iterations++;
sec -= vals[i];
}
if (iterations > 0) {
res += iterations + " " + strs[i] + ", "}
}
if (sec > 0) {res += sec + " sec"}
//if not it removes the additional ", ". -2 is reffered as //res.length - 2
else {return res.slice(0, -2)}
return res
}
console.log(convertSeconds(6000000))
``````
`````` function convertSeconds(sec) {
var durSuff = ["sec", "min", "hr", "d", "wk"].reverse();
var conRat = [1, 60, 3600, 86400, 604800].reverse();
var tS = sec, cT = 0,  fS = "";

conRat.forEach((el,i) => {
cT = 0;
while(tS >= el && el > 1) { cT++; tS -= el; }
if(cT > 0) { fS += `\${cT} \${durSuff[i]}, `; }  });
if(tS > 0 && !(cT > 0)){ fS += `\${tS} sec`;}
else{ return fS.slice(0, fS.length-2); }
return fS; }
``````

/*Derived from my FCC forum post about this, aided by harel_avv.

URL: Convert Seconds to Compound Rosetta: Formatting is Wrong. I scrapped the backwards for-j traversal by reversing the arrays, then using forEach loop to try to minimize complexity… */

Yeah, … it’s done with a different style, but those specific conditions (in fE loop out of while / out of the fE loop wholly ) can’t be jammed anywhere else w/o going loco… Thanks for the assist, but I’m now wondering something. Is there a way to completely guarantee that the code runs, for all efficiencies O(n*m) ->~= O(n) , regardless of BC, mid, WCS’s?

1 Like