I came across this project lodash somehow and I was wondering what the point of it is?
I was reading the instructions and I’m not sure when I’m suppose to start coding but I started during step 3 already and here’s what I got
const _ = {
clamp(number, lowBound, upperBound) {
// // my solution for readability
// if (number < lowBound) {
// return lowBound;
// } else if (number > upperBound) {
// return upperBound;
// } else if (number > lowBound && number < upperBound) {
// return number;
// }
// // my solution for clean code
// return (number < lowBound) ? lowBound : (number > upperBound) ? upperBound : (number > lowBound && number < upperBound) ? number : false;
// // my solution using Math Object
// const clampLow = Math.max(number,lowBound);
// const clampUpper= Math.min(clampLow,upperBound);
// return clampUpper;
// my solution using Math Object but in one line
return Math.min(Math.max(number,lowBound),upperBound);
}
};
const test1 = _.clamp(10,-5,5);
const test2 = _.clamp(-10,-5,5);
const test3 = _.clamp(0,-5,5);
console.log(test1);
console.log(test2);
console.log(test3);
// Do not write or modify code below this line.
module.exports = _;
I was looking at the walkthrough when I finished and tested my code and the solution code instead uses
clamp(number, lower, upper){
var lowerClampedValue = Math.max(number,lower);
var clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
}
Is there a difference? and is there a preference in the industry?
I had 2 solutions for mine as shown above without looking past step 3 I just coded how it was described. Should I use the:
// my solution for readability code
or
// my solution for clean code
or
// walkthrough code from the video?
or
// my solution using Math Object but in one line
It is a project so you can use whatever code you like as longs as it works. Have you tested it?
There is no right or wrong answer when it comes to programming. As a result, don’t be frustrated if the solution we present is different than the solution you came up with. We are merely trying to challenge you to consider many different solutions. Your solution is equally as valid as ours. Consider the one you were going to write and then consider ours. Whichever you pick in the end is completely your decision, and we support it completely.
To what I can see, you seem to have a higher understanding of Javascript over what is needed for the project. Your methodology of using shorthand notations and existing methods, and questioning what code to use, shows this.
It is up to you to see whether this project is challenging you enough, if not, see if you can find a way how to make things more challenging, and if that doesn’t work, then this course may not be for you…
Hey! I got this same error, but I figured it out by console.log - ing all my variables to determine what the problem actually was. If the end padding length ends up negative, this will be thrown.
You have a couple of issues with your if statement at the beginning:
if (string.length <= string)
You should be checking if string.length is greater than >, not less than <.
You should not be comparing string.length to string, you should be comparing it to length parameter.
View correct code below and compare:
if (string.length >= length)
Also, paddingString statement looks ok, nothing jumps out to me, so a little strange you are getting this error. Have you tried just returning statement instead of passing it to a variable:
confused at what i’m doing wrong in this project. I’m currently on the clamp portion of the project and i’m already stuck.
It’s giving me errors and telling me that im using unexpected tokens like var, so i tried let, and then const and none of them work even though the steps tell us to create variables inside of the method…
const _ = {
clamp(number,lower,upper) {
var lowerClampedValue = Math.max(number, lower),
var clampedValue = Math.min(lowerClampedValue,upper),
return clampedValue,
}
};
first i tried building clamp with a decimal in front, and then with an underscore. they seem interchangeable but i’m still getting errors. I decided to remove them and just put clamp but still get errors about my variable. any insight?
When you receive an unexpected token error, the problem is very frequently with the line above where the error is thrown, specifically, the very end of the line above. In your case, the line of code referenced in the error message is;
var clampedValue = Math.min(lowerClampedValue, upper),
What does the line above it end with?
Hint
It ends with a comma (so do your other lines).
Additional Hint
A comma is not a semicolon.
var, let & const are all unexpected following a comma in this instance. (Note: you could use const since the values don’t change after the variables are assigned, but let or var will also work)
That is an incredibly valuable piece of information. Thank you so much for sharing that, i thought for the longest time it had to do with the line mentioned. Game changer!
Pad function adds spacing to either side of the word, right?
So you’ll first want to start with figuring out the different between desired length and length of the word(string)
Got another question a few parts down: with the _.has function, I’m a bit confused as to how to go by this one, I’ve gone with a few attempted solutions but each of them are incorrect, and I’m hands down confused as to what it wants me to solve.
Postnote. This is just me trying to find some possible way to solve it, I had different, easier and more difficult ways to do it but this is what I had at the time of writing.
Yes, it’s tested, but as with each other test it either marks all inputs as true or false, but not either, always the same one for a test, meaning i either get Test 1 and 3 or 1 and 2 correct.