Solving The Azure Functions Challenge With JavaScript

Burke Holland
Jun 15, 2017 · 8 min read

Ping Pong

module.exports = function (context, req) {    // Check if we got a ping property
if(req.body && req.body.ping) {
context.res = {
body: { 'pong': data.ping }
};
}
else {
context.res = {
status: 400,
body: { error: 'You gotta ping to pong'}
};
}
context.done();
}

Decipher Text

Loop

// break the msg into an array of 2 digit numbers
let result = [];
for (var x = 0; x < data.msg.length; x += 2) {
result.push(data.msg.substring(x, x + 2));
}

Regex

// break the msg into an array of two digit numbers
let result = req.body.msg.match(/.{1,2}/g);

Installing NPM Modules In Azure Functions

let invert = (obj) => {
var ret = {};
for(var key in obj){
ret[obj[key]] = key;
}
return ret;
}
var _ = require('underscore');module.exports = function (context, req) {    if (req.body && req.body.key && req.body.msg && req.body.cipher) {

// break the msg into an array of two digit numbers
let twoDigitSeries = req.body.msg.match(/.{1,2}/g);

// invert the object/keys on the cipher to make it easy to
// read with array notation
let invertedCipher = _.invert(req.body.cipher);
// iterate over the two digit series and use
// the cipher to decode the message
let result = '';
twoDigitSeries.forEach((item) => {
result += invertedCipher[item];
});
context.res = {
// status: 200, /* Defaults to 200 */
body: { "key": req.body.key, "result": result }
};
}
else {
context.res = {
status: 400,
body: "Please pass key, msg and cipher parameters"
};
}
context.done();
};

Sort Array

Step 1

module.exports = function (context, req) {    if (req.body && req.body.key && req.body.ArrayOfValues) {        context.bindings.outputTable = { 
PartitionKey: 'MyPartitionKey',
RowKey: req.body.key,
ArrayOfValues: req.body.ArrayOfValues
}
context.res = {
body: {}
}
}
else {
context.res = {
status: 400,
body: "Please pass a key and ArrayOfValues parameters"
};
}
context.done();
};

Step 2

module.exports = function (context, req) {    if (req.body && req.body.key) {        let key = req.body.key;
let ArrayOfValues = JSON.parse(context.bindings.inputTable.ArrayOfValues);
// sort the array
let sortedArray = ArrayOfValues.sort((a, b) => { return a - b });
context.res = {
// status: 200, /* Defaults to 200 */
body: { key: key, ArrayOfValues: sortedArray }
};
}
else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
context.done();
};

GitHub Deploy

Challenge Accepted, Challenge Completed

Thanks to Faiz Shaikh

Burke Holland

Written by

Pretty fly for a bald guy. Hacking on Azure at Microsoft.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade