Codewars | You're A Square! (JavaScript)

JavaScript
Transcript

English (Auto-generated)

Hello and welcome back to another cada. So today we're going to be doing another one of the javascript code war Caritas and we're gonna be doing the your a square. It's one of the ones in the javascript fundamentals. Um, and let's just go ahead and tackle it. So it says you like building blocks, especially like building blocks that are square is the way you even like more is to arrange them into a square of square building blocks. However, sometimes you can't arrange them into a square instead you end up with an ordinary rectangle, those blasted things. If you just had a way to know whether you're currently working in vain. Wait, that's it. You just have to check if your number of building blocks is a perfect square. Given an integral number, determine if it's a square number in mathematics, a square number or a perfect square is an integer, that is the square of an integer. In other words it is the product of some integer with itself. The test will always use integral numbers. So don't worry about that in dynamic type languages. So let's go ahead and check out our tests here. So basically what we're going to be checking for is that is going to be a square. So we can't have negative numbers because they cannot be squared numbers. Um, so it needs to be equal to or greater than zero because zero is a square number. And then we just have to write the formula for checking if it's a square. So in here, what I'm gonna do is just thinking about this here. Um Okay let's write an if statement. Mhm And then we'll say else false and if our and this is going to return true and that if it is a square. So what we want to check first of all is if n is greater than or equal to zero. So we want that to be true and we want to check four, it was a square number. So what we're gonna do is we're gonna use the math one of the math functions um it's a square root function. So math dot believe it's court that starts work. Okay, squirt transit squared over the root of a number. Perfect. So return the square root of that number. But we're gonna want to check if the number is actually like a whole number. So what we're going to want to do is we're gonna want to make sure that the like the remainder I'm divided by one, it's the remainder is going to be zero. So Perfect. Go ahead and input that one. We passed all the tests there. So if n is greater than zero or greater than or equal to zero. And if the square root of n divided by one Is equal to remains remains zero. Sorry, describing that very badly with the remainder function but basically we're making sure the remainder is um there's no extra remainder. It divides equally. So Perfect. Go ahead and submit that. Excellent. And that is our solution to the square root or the euro square cada. Um, that's a similar number, isn't. There is a good way to check this as well. So essentially doing the same thing here is just checking that it's an integer and not afloat, so it's a float. Then it won't be the square root there, so Perfect.
952 Views 1 Likes 0 Comments

We work together to solve one of the Kata's over on the Codewars website. Today we're solving the "You're A Square!" JavaScript kata. Check out the Cod...

Comment
Leave a comment (supports markdown format)