Thanks for sharing your solutions! I love seeing what other people come up with because it makes me a better programmer. My code passed the tests on FCC, and I ran it myself with both zero and a negative number. In both instances I got back an empty string.
That's because it never gets to the if (Math.sign(num)) bit in your example.
for(leti=0;i<num;i++){...}
Initially i === 0, so the guard is initially 0 < num, which will be false if num <= 0.
But you can easily test the behavior of Math.sign.
if(Math.sign(-1)){console.log('Math.sign(-1) is true');}if(Math.sign(1)){console.log('Math.sign(1) is true');}// Output:// Math.sign(-1) is true.// Math.sign(1) is true.
I'm using Math.sign() to check whether or not the number is positive or negative. If it never gets to my if statement, then how is it updating the variable and returning the string the correct number of times?
I guess I only call Math.sign for the positive case, the else statement returns the empty string for the other cases. I still don't see how this isn't a correct solution to the problem. I check to see if it is a positive number with Math.sign, if it is the variable gets updated and returned the correct number of times. In all other cases, negative or zero, it returns an empty string.
Math.sign(-1) is -1, which is true, so your example code will accept negative numbers, and only reject 0.
What you have written is equivalent to if (num !== 0) { ... }
I recommend writing if (num > 0) { ... } given what you've written in the comments.
If you're doing this, you could just replace the whole thing with a simple recursive expression.
But using + iteratively on strings produces a lot of intermediary junk, so I'd suggest using join instead.
But the simplest approach is probably this. :)
Thanks for sharing your solutions! I love seeing what other people come up with because it makes me a better programmer. My code passed the tests on FCC, and I ran it myself with both zero and a negative number. In both instances I got back an empty string.
That's because it never gets to the if (Math.sign(num)) bit in your example.
Initially i === 0, so the guard is initially 0 < num, which will be false if num <= 0.
But you can easily test the behavior of Math.sign.
"Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number."
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
In what case do you think you call Math.sign with a value less than one?
I'm using Math.sign() to check whether or not the number is positive or negative. If it never gets to my if statement, then how is it updating the variable and returning the string the correct number of times?
I guess I only call Math.sign for the positive case, the else statement returns the empty string for the other cases. I still don't see how this isn't a correct solution to the problem. I check to see if it is a positive number with Math.sign, if it is the variable gets updated and returned the correct number of times. In all other cases, negative or zero, it returns an empty string.
So, if (Math.sign(num)) { . . . } could be replaced with if (true) { ... } in your code without changing any answer?