Skip to content

Instantly share code, notes, and snippets.

@CliffCrerar
Created April 17, 2020 20:43
Show Gist options
  • Save CliffCrerar/9c392b3eeb13883fd25200738a02a4d9 to your computer and use it in GitHub Desktop.
Save CliffCrerar/9c392b3eeb13883fd25200738a02a4d9 to your computer and use it in GitHub Desktop.
/*
Highest Value Hour Glass Solutions
Author: Cliff Crerar
*/
// function: determines the current our glass
// - will be executed during iterations
// - takes the xc and yc parameter which are the two parts of a co-ordinate
// - then from that origin and returns it's sum to the current iteration
// - r is the two dimensional array or table
function calculateOne(xc, yc, r) { // function statement
try { // error handles the function of any of the positions return undefined
/* Hour glasses are defined by 7 co-ordinates */
return [
/* position 0 */r[yc][xc],
/* position 1 */r[yc][xc + 1],
/* position 2 */r[yc][xc + 2],
/* position 3 */r[yc + 1][xc + 1],
/* position 4 */r[yc + 2][xc],
/* position 5 */r[yc + 2][xc + 1],
/* position 6 */r[yc + 2][xc + 2]
]
// uses a reducer function to sum the values in the hourglass array
.reduce((total, next) => {
if (next === null) // if value is null then throw error
throw new Error('No position can be undefined');
return total += next
});
} catch (err) {
return null;
// if error is thrown function returns null
// it is bad practice to use undefined
// undefined is a type that can be use to evaluation but
// must never be explicitly assigned
//
// WRONG:
// let variable = undefined
// OK:
// if(variable===undefined) { } else { }
}
}
function solution(arr) {
const // declarations
xMax = arr[0].length,
yMax = arr.length;
let
h = 0; // highest value starts at highest value 0;
console.log('length of X: ', xMax); // logs the number of columns
console.log('length of Y: ', yMax); // logs the number of rows
for (let y = 0; y < arr.length; y++) {
for (let x = 0; x < arr[y].length; x++) {
const i = calculateOne(x, y, arr);
h = i === null ? h : i > h ? i : h; // nested ternary statements
// above is called a ternary statement,
// condition ? *value if true* : *value if false*
// just syntactic sugar and more concise than a
/**
if(condition){
do something if true
} else {
do something if false
}
*/
}
}
return h; // write pure functions, always use return even if return void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment