Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.
function test(xval) {
var n = xval.length, i = 0;
while (++i < n)
if (xval[i - 1] >= xval[i])
return false;
return true;
}
let A = test([2,6,6,9])
// A = ? (boolean)
This seems like an easy challenge, I hope we can finish it quickly. It starts with a call to an unknown function test
that takes an array as sole argument. We have no clue what this function does so we'll have to analyze it:
var n = xval.length, i = 0;
while (++i < n)
These first two lines basically just iterate over each element in the array, but it's slightly different because it skips the first element.
The final 3 lines are:
while(...)
if (xval[i - 1] >= xval[i])
return false;
return true;
The if-condition checks if the previous value is greater than or equal to the current value. If so, it immediately returns false. If this if-condition never passes, the function returns true.
Basically this function checks if each value in the array is strictly greater than the previous one. The input used in this challenge is [2,6,6,9]
, it has a duplicate 6 so the array does not have strictly greater ascending values.
By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/
Top comments (2)
Can you provide me the link? I want to learn code by challenge too
I have tried some, but i can solve it
Hey, the link to codr is in the article and on my profile; nevolin.be/codr/