https://www.codewars.com/kata/persistent-bugger/train/javascript I'm working on the JavaScript Code Wars Persistent Bugger problem. As seen in the code, my if-statement appears to completely ignore the for-loop within it.
Anything logged within the for-loop does not show, and anything logged outside the for-loop remained the same as if the for-loop wasn't there. I've tried researching the use of for-loops inside if-statements as well as existing solutions for this Code Wars problem, but did not see my issue being replicated. From my understanding, the if-statement should run through everything within it from top to bottom.
const persistence = (num) => {
if (typeof(num) === 'number') {
let count = 0
let mult = num
let newMult = 1
let stop = false
while (stop === false){
if (mult >= 10) {
for (let i = 0; i < mult.length; i++) {
newMult = newMult * mult[i]
// // doesn't log anything. code isn't running through for loop?
// console.log(newMult)
}
// // logs initial num of 999
// console.log(mult)
// // logs initial newMult of 1
// console.log(newMult)
count++
mult = newMult
newMult = 1
}
else {
stop = true
}
}
return count
}
}
// logs 1, should be 4
console.log(persistence(999))
I think I have the logic correct, but maybe there's some fundamental issue I'm missing in regards to how if-statements and for-loops work. I noticed there are much simpler ways to solve this problem, but if I could receive feedback on why my specific code doesn't work, and how I could tweak it to make it work, that would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire