Posts

Showing posts with the label function

Python 3: Input in cmd to return functions

Python 3: Input in cmd to return functions I'm currently working my way through "Learn Python the Hard Way" and got to the first exercise about functions. It's simply creating a few functions and printing them out like in the previous examples in the book`. Code: def print_two(*args): arg1, arg2 = args print("arg1: %r, arg2: %r" % (arg1, arg2)) def print_two_again(arg1, arg2): print("arg1: %r, arg2: %r" % (arg1, arg2)) def print_one(arg1): print("arg1: %r" % (arg1)) def print_none(): print("I got nothing.") print_two("Zed","Shaw") print_two_again("Zed","Shaw") print_one("First!") print_none() Output in cmd: C:Users[USER]Google DrivePythonLearn Python the Hard Way>python ex18.py arg1: 'Zed', arg2: 'Shaw' arg1: 'Zed', arg2: 'Shaw' arg1: 'First!' I got nothing. I want to play around a bit with this, so instead of just gi...

`“n”` can't run in `new Function()`

`“n”` can't run in `new Function()` I want to run code dynamiclly, without using <script> ,so I used new Function ,but there is a problem when I write n : <script> new Function n function run (code) { (new Function(code))() } run('console.log("run well")') // it work well run('console.log("nError")') // error the result is: error Uncaught SyntaxError: Invalid or unexpected token at new Function (<anonymous>) at run (<anonymous>:2:3) at <anonymous>:1:1 And we can find the reason in console: the 'n' has been turn to a new line (function() { console.log(" // error here Error") }) so using ` to replace " can solve this problem: run(`console.log("nWell")`) (function() { console.log(` // work here Error`) }) but it is not suitable that using ` in production, so if there other way to let it well done ? Use double backslashes to indicate a li...

How to pass a function that's a property of another object into a function and call it in javascript?

How to pass a function that's a property of another object into a function and call it in javascript? If I have an object in Javascript and one of its properties is a function: function cow() { this.timesMooed = 0; this.sayMoo = function () { this.timesMooed++; return "moo"; }; } Say I also have another function that takes some function as an argument, calls it and records the result: var actionResults = ; function doAction(action) { actionResults.push(action()); } Now let's put this into practice and see what happens: var jerry = new cow(); doAction(jerry.sayMoo); console.log(actionResults); // Outputs ["moo"] -this is correct console.log(jerry.timesMooed); // Outputs 0 -uh oh How can I pass in the function so that it's Jerry that is running the function? Jerry doAction(jerry.sayMoo.bind(jerry)); – ASDFGerte Jul 2 at 2:25 ...

AS3: Adding function to population loop

AS3: Adding function to population loop Basically I have 2 movieclip objects with some code, currently just to trace them. The blue circles when clicked will say 'Blue' and the red ones when clicked will say 'Red'. This works fine in theory until I add a population loop, which adds more of them. Then only 1 of each colour correctly works, the rest are just 'mock' circles. I wish for each circle to tell me their colour. This is my code for the .fla: import flash.events.MouseEvent; BlueBall.addEventListener(MouseEvent.CLICK, fun1) function fun1(e:MouseEvent){ trace("Blue!"); } RedBall.addEventListener(MouseEvent.CLICK, fun2) function fun2(e:MouseEvent){ trace("Red!"); } and this is the population loop in an .as file: private function PopulateCircles():void { for (var i:int=0; i < 10; i++) { var blueCircle:BlueCircle = new BlueCircle(); this.addChild(blueCircle); var redCircle:RedCircle = new RedCircle(...

Recursively calculate the sum of an Array of integers in JavaScript

Recursively calculate the sum of an Array of integers in JavaScript I wanted to write a JavaScript program to compute the sum of an array of integers Recursively . Expected Results Input : [1, 2, 3, 4, 5, 6] Output : 21 I achieved the above results with this code: function calculateSum(array) { if (array instanceof Array){ if (!array.some(isNaN)) { var total = 0; array.forEach(function (value) { total += value; }); return total; } return "Provide an Array with only Numeric Values"; } return "Please provide an Array"; } But I'm looking for a solution that uses Recursion . EDIT : I started doing the above exercise to practice Recursion . I was having a hard time figuring that out. So, That's why I posted this. I'd be glad if you understood. Thanks in advance. What have you tried? What specifically do you need help with? ...

How to run a function with optional argument/parameter in Google Apps Script

How to run a function with optional argument/parameter in Google Apps Script I am trying to run a function which has an optional argument/parameter in Google Apps Script. The function will create a Google Document and will also create a Google Slide if the user selects Yes. My problem is when the extractReplace function is being called, an error will occur because the copySlide is not defined. Here's how I'm calling the function: var copyId = DriveApp.getFileById(documentTemplate) .makeCopy(documentName + folderName, nsdestinationFolder) .getId(); var copyDocument = DocumentApp.openById(copyId); var copyBody = copyDocument.getActiveSection(); //call function to create technical slide proposal if(createTemplate == "Yes"){ createSlideProposal(); } else{ extractReplace(copyBody, copySlide = 0); } I have tried copySlide = 0, false, null, but it did not work as the copySlide is not defined when is passes to the copySlide.replaceAllText ...