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;
}
Your browser information:
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.
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] + ", "}
}
//adds additional seconds if left
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))
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?