For…in and Math.abs in JavaScript. Codingame: Temperatures – Solved

Hi! Let’s play and solve a puzzle titled “Temperatures” by Codingame.
I’m Anatol and you are watching the Good Parts of the FrontEnd development.

The story tells: It’s freezing cold out there! Will you be able to find the temperature closest to zero in a set of temperatures readings?

This puzzle teaches you how to compare a list of values and an Absolute value.

To win this game my program must analyze records of temperatures to find the closest to zero.

Let’s start with one trick. I have a string of values, separated by whitespaces. I create an array from this string using a “split” operator. I omit brackets and use the backtick (a template literal) instead. This function makes code a bit shorter.

Let’s use a “for…in” for looping. The “for…in” statement iterates over the enumerable properties of an object. For each distinct property, statements can be executed.

Be aware, that for…in should not be used to iterate over an Array where the index order is important. Array indexes are enumerable properties with integer names and there is no guarantee that for…in will return the indexes in any particular order. But this isn’t our case.

Besides of that I use Math method “absolute”. It returns an absolute value derived from a given variable or function.

And one more useful shorthand: Logical OR. This means: assign to a variable “a” the value of variable “b”. When variable “b” is undefined, use a value of a variable “c”.

My code is available on GitHub
Documentation is available in the Internet
Follow the links if you’re eager to get more.

If you like this video give it “thumbs up”. Thanks for watching and dive deeper.
Subscribe to the channel:
Watch other episodes:
Follow on Twitter:

Mendum by Jakebur4, Belles Bells by Loupin, Video Game Forest by AcidParadox
Arcade Town, Police Radio Sound, Small Bell by Alexander
Come and Find Me by Eric Skiff

frontend development, frontend, javascript, css, design, performance, angularjs, react, reactjs, node, nodejs, performance optimization, learn javascript, javascript tutorial, vue, vuejs, ember, emberjs, programming, programming tutorial, code, coding, debug, debugging, web, time-management, web programming, game, gamedev, game development

Xem Thêm Bài Viết Công Công Nghệ:


Add Comment