The objective of this code is to write the rev function and make it return the following Obviously its
Maybe that's what you wanted. Since you are passing a function as a parameter you are using high order function or a decorator , hope this helps
check this here
function welcome(name) {
return `Welcome ${name}`;
}
function bye(name) {
return `Bye ${name}`;
}
function rev(wrapped) {
return function() {
const result = wrapped.apply(this,arguments);
return `${result}, ${result.split(" ").reverse().join(" ")}`
}
}
const revWelcome = rev(welcome);
const revBye = rev(bye);
console.log(revWelcome('James'))
console.log(revBye('Bond'))
Related
In this exercise I can't figure out why if I return the function I get an error, while if I return the function as a literal object it works
(module.js not works)
module.exports = {eleva : allaSeconda()}
function allaSeconda(){
function calcola(num){
return num*num
}
return calcola
}
(module.js works)
module.exports = {eleva : allaSeconda()}
function allaSeconda(){
function calcola(num){
return num*num
}
return {calcola}
}
(index.js)
const {eleva} = require('./modulo')
console.log(eleva.calcola(9))
Your syntax is a little bit convoluted, you can simply:
// no need for parenthesis in the exports.
// you export a function that elevates
module.exports = {eleva : allaSeconda}
function allaSeconda(num){
return num*num
}
Which could also be done totally inline:
// no need for parenthesis in the exports.
// you export a function that elevates
module.exports = {
eleva: function(num) {
return num * num;
}
}
And you call it simply like so:
const {eleva} = require('./modulo')
console.log(eleva(9))
Please note that your first example works, if you call eleva directly.
I need to recreate all functions starting with getXXX in the Alexa object on a higher level object (handlerInput) following this pattern:
handlerInput.getLocale = function getLocale() {
return Alexa.getLocale(handlerInput.requestEnvelope);
}
handlerInput.getRequestType = function getRequestType() {
return Alexa.getRequestType(handlerInput.requestEnvelope);
}
handlerInput.getIntentName = function getIntentName() {
return Alexa.getIntentName(handlerInput.requestEnvelope);
}
handlerInput.getAccountLinkingAccessToken = function getAccountLinkingAccessToken() {
return Alexa.getAccountLinkingAccessToken(handlerInput.requestEnvelope);
}
handlerInput.getApiAccessToken = function getApiAccessToken() {
return Alexa.getApiAccessToken(handlerInput.requestEnvelope);
}
handlerInput.getDeviceId = function getDeviceId() {
return Alexa.getDeviceId(handlerInput.requestEnvelope);
}
handlerInput.getUserId = function getUserId() {
return Alexa.getUserId(handlerInput.requestEnvelope);
}
handlerInput.getDialogState = function getDialogState() {
return Alexa.getDialogState(handlerInput.requestEnvelope);
}
handlerInput.getSupportedInterfaces = function getSupportedInterfaces() {
return Alexa.getSupportedInterfaces(handlerInput.requestEnvelope);
}
handlerInput.isNewSession = function isNewSession() {
return Alexa.isNewSession(handlerInput.requestEnvelope);
}
handlerInput.getSlot = function getSlot(slotName) {
return Alexa.getSlot(handlerInput.requestEnvelope, slotName);
}
handlerInput.getSlotValue = function getSlotValue(slotName) {
return Alexa.getSlotValue(handlerInput.requestEnvelope, slotName);
}
handlerInput.escapeXmlCharacters = function escapeXmlCharacters(input) {
return Alexa.escapeXmlCharacters(input);
}
handlerInput.getViewportOrientation = function getViewportOrientation(width, height) {
return Alexa.getViewportOrientation(handlerInput.requestEnvelope, width, height);
}
handlerInput.getViewportSizeGroup = function getViewportSizeGroup(size) {
return Alexa.getViewportSizeGroup(size);
}
handlerInput.getViewportDpiGroup = function getViewportDpiGroup(dpi) {
return Alexa.getViewportDpiGroup(dpi);
}
handlerInput.getViewportProfile = function getViewportProfile() {
return Alexa.getViewportProfile(handlerInput.requestEnvelope);
}
So in my code instead of using Alexa.getLocale(handlerInput.requestEnvelope) I would just do handlerInput.getLocale(). I can't modify these functions as they are part of an SDK. My runtime is node 8. The code above works but I was wondering if there's a way to shorten the code and use the pattern to do this in bulk (maybe with a for each function detecting the functions starting with get). BTW note that most functions just take handlerInput.requestEnvelope as parameter but some take a string only and some take handlerInput.requestEnvelope plus extra parameters.
You can do a lot with loops and closures:
for (const method of ["getLocale", "getRequestType", "getIntentName", "getAccountLinkingAccessToken", "getApiAccessToken", "getDeviceId", "getUserId", "getDialogState", "getSupportedInterfaces", "isNewSession", "getSlot", "getSlotValue", "getViewportOrientation", "getViewportProfile"]) {
handlerInput[method] = function(...args) {
return Alexa[method](handlerInput.requestEnvelope, ...args);
};
}
for (const method of ["escapeXmlCharacters", "getViewportSizeGroup", "getViewportDpiGroup"]) {
handlerInput[method] = Alexa[method].bind(Alexa);
}
Depending on your requirements and the Alexa object, you might also simply enumerate all existing properties.
Not sure if this is the right title but should be quick help.
Pretty much I keep on getting errors on testing a function because "TypeError: ParseThis.changeIt is not a function". Here's my code. What am I missing that causing this type error? Thanks!
const ParseThis = () => {
const changeIt = string => string;
return { changeIt: changeIt() }
}
Edit: More details!
Thanks for the help again
When you return your object, maybe you wanted to return the function and not the result of the call:
return { changeIt: changeIt };
or this which is more concise:
return { changeIt };
According to how you are using the translate function, I think you should export it this way:
const Translator = {
const translate = string => string;
};
if (module.exports) {
module.exports = Translator;
}
or this way:
const Translator = () => {
const translate = string => string;
return { translate };
}
if (module.exports) {
module.exports = Translator();
}
Return the function instead of calling it.
const ParseThis = () => {
const changeIt = string => string;
return { changeIt };
}
In the original post, changeIt() is a call to changeIt with no first parameter. It returns the value undefined. To return a function instead of calling it, omit the parenthesis.
Let's analyze your code.
Let's start from this:
const changeIt = string => string;
At this point, changeIt is a function that, given a parameter, it returns that a parameter.
Without an arrow function, if we should use the old classic named function, it would be like this:
function changeIt(parameter) {
return parameter;
}
What happens if you call changeIt() with no parameter? In javascript, when you pass no parameters ot a function, it's like you are passing undefined. So the function will return undefined.
Then you have this line:
return { changeIt: changeIt() }
But as we have seen, changeIt() is equal to undefined. So your code is equivalent to:
return { changeIt: undefined }
Which is clearly not a function!
What you probably meant to do, is not returning the result of function invokation, but return the function itself. So, instead that assigning changeIt(), just assign changeIt:
return { changeIt: changeIt }
Notice that you repeated twice the word changeIt, so you can get rid of this repetition and just write:
return { changeIt }
Your function is returning an object, so instead of
ParseThis.changeIt()
You should be doing something like
const a = ParseThis();
a.changeIt('some string');
But, note that even in your example, changeIt in the returning object is not a function.
Probably you are trying this
const ParseThis = () => {
const changeIt = string => string;
return { changeIt: changeIt};
}
Note that I've used { changeIt: changeIt}, setting changeIt to a reference of the inner function changeIt. And you are using { changeIt: changeIt()} setting changeIt to the value returned of the inner function changeIt. Those are two different operations.
The problem is that you are exporting a function, and not the object containing the nop function. You need to add parenthesis to your dummy:
const Translator = () => {
const translate = string => string;
return { translate };
};
if (module.exports) {
module.exports = Translator(); // add parenthesis here
}
Alternatively you could run the function you import, but I suspect that would be different from your real Translator api.
I have this method:
wordFormDirty = (): boolean => {
var self = this;
angular.forEach(self.word.wordForms, function (wf, key) {
var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId
if (!self[wordFormNgForm].$pristine) {
return true;
}
});
return false;
};
From what I see this never returns true. Can someone give me advice as to how I can implement this so that a form that's not pristine will make the wordFormDirty() method return true.
can you try this, in this case, if I've undestand the issue, the first time there is a value true the result is set to true otherwise it remains false
wordFormDirty = (): boolean => {
var self = this;
var result = false;
angular.forEach(self.word.wordForms, function (wf, key) {
var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId
if (!self[wordFormNgForm].$pristine) {
result = true;
}
});
return result;
};
If you wish to get a result directly from walking the Array, consider using other methods than forEach. E.g.:
return Object.values(this.word.wordForms).some(
({ wordFormId }) => !this[`wordFormNgForm_${wordFormId}`].$pristine
);
I am trying to create a higher order function in javascript someMathArray(x) { } that returns a function and takes another single argument. I want the function to take an original array, say [1,2,3,4] then apply another function for example, named mult2(a) { return a*2 }(however, I want this to work for any function I pass in. I don't want mult2(a) to be hard coded into the function) and then return an array containing [2,4,6,8]
Something like this?
function someMathArray(array) {
return function(fun) {
return array.map(fun);
};
}
var fun = someMathArray([1,2,3,4,5]);
var output = fun(function(a) { return a*2; });
document.getElementById('output').innerHTML = JSON.stringify(output);
<div id="output"></div>
or
function someMathArray(array) {
return array.map.bind(array);
}
var fun = someMathArray([1,2,3,4,5]);
var output = fun(function(a) { return a*2; });
document.getElementById('output').innerHTML = JSON.stringify(output);
<div id="output"></div>