I want to bring out the results of a function that is within another function. When I try to print or return results it only brings out the result of the function "Sum".
let readlineSync = require("readline-sync");
let a = readlineSync.question(
"Choose an operation: Sum or Substraction: "
);
let param1 = parseInt(readlineSync.question("Value 1: "));
let param2 = parseInt(readlineSync.question("Value 2: "));
chosenFunction();
function Sum() {
return param1 + param2;
}
function Substraction() {
return param1 - param2;
}
function chosenFunction() {
let result;
if (a = 'Sum') {
result = console.log (Sum());
} else if (a = 'Substraction') {
result = console.log ( Substraction());
}
return result
}
It's an invalid usage,when you assign value,you need to remove console.log
Also need to change = to == when compare values
So change
result = console.log (Sum());
result = console.log ( Substraction());
to
result = Sum();
result = Substraction();
Full code
function chosenFunction() {
let result;
if (a == 'Sum') {
result = Sum();
} else if (a == 'Substraction') {
result = Substraction();
}
return result
}
You need to use == or ===. You are actually changing your values, not comparing, when using = as in else if (a = 'Substraction').
if (a === 'Sum')
else if (a === 'Substraction')
Related
Putting together a simple script to test out a more complex math operation in JavaScript. The goal of the function is to return a function that will add an input by x.
For example:
const addByTwo = addByX(2);
addByTwo(1); //should return 3
addByTwo(2); //should return 4
addByTwo(3); //should return 5
const addByThree = addByX(3);
addByThree(1); //should return 4
addByThree(2); //should return 5
Here's my function:
function addByX (input) {
function output () {
return input + 2;
}
return output;
}
const addByTwo = addByX(2);
Error:
I'm currently getting: Syntax Error: Unexpected identifier
Test code:
console.log(addByTwo(1));
console.log(addByTwo(2));
On what line are you getting a SyntaxError? Your definition of addByX appears to be incorrect. It should be as follows:
function addByX(x) {
function output(input) {
return input + x;
}
return output;
}
const addByTwo = addByX(2);
addByTwo(1); //should return 3
addByTwo(2); //should return 4
addByTwo(3); //should return 5
const addByThree = addByX(3);
addByThree(1); //should return 4
addByThree(2); //should return 5
console.log(addByTwo(1));
console.log(addByTwo(2));
define your addByX function like this:
function addByX (x) {
return function output (input) {
return x + input;
}
}
const addByTwo = addByX(2);
const addOne = addByTwo(1);
console.log(addOne);
you can write a function like this
Function:
function addByX(fun_name, input)
{
var add = 0;
if (fun_name == "Two") add=2;
else if (fun_name == "Three") add = 3;
return add + input;
}
Usage:
addByX("Two", 1);
addByX("Three", 1);
I'm just writing a very simple function in javascript to calculate a factorial. I understand that in javascript you can assign a function to a variable.
So I have tried this on an online compiler (https://repl.it/languages/javascript) and this is what my code looks like
var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
console.log(mynum +" is not a number");
}
else{
console.log("You entered "+mynum);
answer = function (mynum){
var i = mynum-1;
var temp = mynum;
while(i>0){
temp = temp*i;
i--;
}
return temp;
};
console.log("the factorial of "+mynum+" is "+answer);
}
But when I run this the output keeps including the whole function as "answer"
You entered 23
the factorial of 23 is function (mynum) {var _loopStart = Date.now(),_loopIt = 0;
var i = mynum - 1;
var temp = mynum;setTimeout(function () {_loopStart = Infinity;});
while (i > 0) {if (++_loopIt > 5000 && Date.now() - _loopStart > 150) throw new RangeError("Potential infinite loop. You can disable this from settings.");
temp = temp * i;
i--;
}
return temp;
}
However i don't have this issue when i create the function and then call it separately (something like answer = function(mynum).
Can anyone please let me know why this is happening?
Thanks!
Assigning a function to a variable is different from assigning its evaluation.
In your case, you have two solutions available :
Make an effective call to your assigned function at logging time:
console.log("the factorial of "+mynum+" is "+answer(mynum));
Make an effective call to your assigned function at assignation time:
answer = (function (mynum){
var i = mynum-1;
var temp = mynum;
while(i > 0) {
temp = temp*i;
i--;
}
return temp;
}(mynum));
Both solutions are quite equivalent for your specific situation.
Why?
Because declaring a function like so:
var func = function () {
console.log("Hello!");
};
Or like so:
function func () {
console.log("Hello!");
};
Has little difference
As pointed out, you have to call it as a function.
var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
console.log(mynum +" is not a number");
}
else{
console.log("You entered "+mynum);
answer = function (mynum){
var i = mynum-1;
var temp = mynum;
while(i>0){
temp = temp*i;
i--;
}
return temp;
};
console.log("the factorial of "+mynum+" is "+answer (mynum));
}
Alternatively, you could use IIEF(mmediately invoked function expression):
var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
console.log(mynum +" is not a number");
}
else{
console.log("You entered "+mynum);
answer = (function (mynum){
var i = mynum-1;
var temp = mynum;
while(i>0){
temp = temp*i;
i--;
}
return temp;
})(mynum);
console.log("the factorial of "+mynum+" is "+answer);
}
Note that I've added a parenthesis around your function and passed in arguments. That's how you can immediately invoke functions.
When you return a function in javascript such as answer(). You must call it as such.
console.log("the factorial of "+mynum+" is "+answer(mynum));
You need to use the function that you wrote by calling it i.e
function add (a,b) {
return a + b;
}
add(1,2);
i am trying to count how many times a letter appear in a string. This is my code:
var myFunc = function(inside) {
count = 0;
for (var i=0;i<inside.length;i++) {
if(inside[i]==="a") {
count+=1;
}
return count;
};
};
console.log(myFunc("hai, okay"));
var myFunc = function(inside) {
count = 0;
for (var i=0;i<inside.length;i++) {
if(inside[i]=="a") {
count+=1;
}
//return should not come here
};
return count;
};
console.log(myFunc("hai, okay"));
or u can use this also
var myFunc = function(inside) {
return (inside.match(new RegExp("a", "g"))).length;
}
console.log(myFunc("hai, okay"));
Your code is not giving correct result since it has a return statement inside a for loop so it will return after first iteration and will return 0. You can simply put the return outside for loop to make it work.
You can also simply change your method to
var myFunc = function(inside, character)
{
return inside.split( character ).length - 1;
};
console.log(myFunc("hai, okay"), "a");
How many letters? As in "Abba" would be 2 letters, namely "a" and "b"?
var letter_counts = function(txt) {
var res = {};
for (var i=0;i<txt.length;i++) {
var c = txt[i].toLowerCase();
res[c] = ( res[c] ? res[c] : 0 ) + 1;
};
return res;
};
var letter_cnts = letter_counts("Abba");
// Object {a: 2, b: 2}
letter_cnts["a"]; // == 2
letter_cnts["b"]; // == 2
What about
var countAs = inside.replace(/[^a]/g, '').length;
Reason: elminate all char's unwanted and take the length of the rest.
Warpped:
function howMany(inside, theChar)
{
var reg = new RegExp("[^" + theChar + "]","g");
return inside.replace(reg, '').length;
}
I have three variables
var r1 = 12;
var r2 = '';
var r3;
I need to make a function convert() with three parameters as so:
function convert(arg1, arg2, arg3) {
// function body here
}
such that after I run this code
convert(r1, r2, r3)
console.log(r1, r2, r3)
I should get the output:
r2, "empty string", "undefined value"
That is it should changed the values of r1, r2 and r3 to the above values.
Code I tried:
function convert(arg1, arg2, arg3) {
if (arg2 === '') {
arg2 = "empty string";
}
if (arg3 === undefined) {
arg3 = "undefined value";
}
}
and then call convert(r1, r2, r3) it fails (obviously!) because of arguments are passed as values. But I need to know is there any way to make this function? Also, I have seen this question, so I know about how they are passed, but still, is there any way I can make this work?
You could do something like this, but most likely something is flawed with your logic.
function convert(arr) {
for (var i = 0, l = arr.length; i < l; i++) {
// do your stuff here on arr[i]
}
return arr;
}
var a = convert([r1, r2, r3]);
//a now holds the right values, do whatever you want with them
//you can even load them back to the variables if that's what you really want
//r1 = a[0]; etc.
I don't know what is the reason to do this, where you want to use these new values, but it would be more clever to write the convert function for only one value, and call it when it's necessary.
function convert(val) {
if (val === '') {
return 'empty string';
}
if (typeof val === 'undefined') {
return 'undefined value';
}
return val;
}
console.log(convert(r1));
You can't do this in JavaScript. It doesn't have pass-by-reference semantics.
You can use an intermediate object fo this, but seems that's not what you are looking for:
var data = {r1: 12, r2: '', r3: undefined};
function convert(data, field1, field2, field3) {
// …
}
In the unlikely event that you only want to convert global variables, you can just pass in the names of those variables:
function convert(arg1, arg2, arg3) {
if (window[arg2] === '') {
window[arg2] = "empty string";
}
if (window[arg3] === undefined) {
window[arg3] = "undefined value";
}
}
var r1 = 12;
var r2 = '';
var r3;
convert('r1', 'r2', 'r3');
console.log(r1, r2, r3);
working code:
fire the execute function on the event you need
function execute(){
var r1 = 12;
var r2 = '';
var r3;
convert(r1,r2,r3);
}
function convert(arg1, arg2, arg3) {
if (arg2 === '') {
arg2 = "empty string";
}
if (arg3 === undefined) {
arg3 = "undefined value";
}
console.log(arg1,arg2,arg3);
// console.log(arg2);
// console.log(arg3);
}
Let's say you have the following function
var action = (function () {
var a = 42;
var b = 2;
function action(c) {
return a + 4 * b + c;
}
return action;
}());
// how would you parse action into it's serialized LISP / AST format?
var parsed = parse(action);
Is it possible to have a function that takes a reference to the function action and outputs say the LISP format (lambda (c) (plus (plus 42 (multiply 4 2)) c))
We're allowed to put some restrictions on what action can be.
the body should only be a single expression
it should be a pure function
any free variables are constants
The main question is given a function you can invoke with a range of inputs and it's source code can you discover the correct value to substitute the free variables with?
For the above example you know that a and b are constant and you could intellectually plot the output for a few values and see the pattern and then just know what the constants are.
Question:
How would you write a function that takes a function reference and it's source code and produces some form of AST for the function with any free variables substituted for their run-time values.
An example of an AST format would be the LISP equivalent of the code.
I basically want to serialize and deserialize the function and have it behave the same
It should be noted that the problem becomes trivial if you pass { a: a, b: b } to the analysis function. That would be cheating.
Use-case:
I want to generate a language agnostic form of a pure JavaScript function so I can effectively pass it to C++ without requiring the user of my library to use a DSL to create this function
Let's imagine you had a database driver
var cursor = db.table("my-table").map(function (row) {
return ["foo", row.foo]
})
You want to determine at run-time what the function is and convert it into an AST format so that you can use your efficient query builder to convert it into SQL or whatever query engine your database has.
This means you don't have to write:
var cursor = db.table("my-table").map(function (rowQueryObject) {
return db.createArray(db.StringConstant("foo"), rowQueryObject.getProperty("foo"))
})
Which is a function the DB library can execute with a query object and have you build the query object transformation without verbose methods.
Here is a full solution (using catalog of variables which is accessible by the parse function):
var CONSTANTS = {
a: 42,
b: 2,
c: 4
};
function test() {
return a + 4 * b + c;
}
function getReturnStatement(func) {
var funcStr = func.toString();
return (/return\s+(.*?);/g).exec(funcStr)[1];
}
function replaceVariables(expr) {
var current = '';
for (var i = 0; i < expr.length; i += 1) {
while (/[a-zA-Z_$]/.test(expr[i]) && i < expr.length) {
current += expr[i];
i += 1;
}
if (isNumber(CONSTANTS[current])) {
expr = expr.replace(current, CONSTANTS[current]);
}
current = '';
}
return expr;
}
function isNumber(arg) {
return !isNaN(parseInt(arg, 10));
}
function tokenize(expr) {
var tokens = [];
for (var i = 0; i < expr.length; i += 1) {
if (isWhitespace(expr[i])) {
continue;
} else if (isOperator(expr[i])) {
tokens.push({
type: 'operator',
value: expr[i]
});
} else if (isParentheses(expr[i])) {
tokens.push({
type: 'parant',
value: expr[i]
});
} else {
var num = '';
while (isNumber(expr[i]) && i < expr.length) {
num += expr[i];
i += 1;
}
i -= 1;
tokens.push({
type: 'number',
value: parseInt(num, 10)
});
}
}
return tokens;
}
function toPrefix(tokens) {
var operandStack = [],
operatorStack = [],
current,
top = function (stack) {
if (stack) {
return stack[stack.length - 1];
}
return undefined;
};
while (tokens.length) {
current = tokens.pop();
if (current.type === 'number') {
operandStack.push(current);
} else if (current.value === '(' ||
!operatorStack.length ||
(getPrecendence(current.value) >
getPrecendence(top(operatorStack).value))) {
operatorStack.push(current);
} else if (current.value === ')') {
while (top(operatorStack).value !== '(') {
var tempOperator = operatorStack.pop(),
right = operandStack.pop(),
left = operandStack.pop();
operandStack.push(tempOperator, left, right);
}
operatorStack.pop();
} else if (getPrecendence(current.value) <=
getPrecendence(top(operatorStack).value)) {
while (operatorStack.length &&
getPrecendence(current.value) <=
getPrecendence(top(operatorStack).value)) {
tempOperator = operatorStack.pop();
right = operandStack.pop();
left = operandStack.pop();
operandStack.push(tempOperator, left, right);
}
}
}
while (operatorStack.length) {
tempOperator = operatorStack.pop();
right = operandStack.pop();
left = operandStack.pop();
operandStack.push(tempOperator, left, right);
}
return operandStack;
}
function isWhitespace(arg) {
return (/^\s$/).test(arg);
}
function isOperator(arg) {
return (/^[*+\/-]$/).test(arg);
}
function isParentheses(arg) {
return (/^[)(]$/).test(arg);
}
function getPrecendence(operator) {
console.log(operator);
switch (operator) {
case '*':
return 4;
case '/':
return 4;
case '+':
return 2;
case '-':
return 2;
default:
return undefined;
}
}
function getLispString(tokens) {
var result = '';
tokens.forEach(function (e) {
if (e)
switch (e.type) {
case 'number':
result += e.value;
break;
case 'parant':
result += e.value;
break;
case 'operator':
result += getOperator(e.value);
break;
default:
break;
}
result += ' ';
});
return result;
}
function getOperator(operator) {
switch (operator) {
case '+':
return 'plus';
case '*':
return 'multiplicate';
case '-':
return 'minus';
case '\\':
return 'divide';
default:
return undefined;
}
}
var res = getReturnStatement(test);
console.log(res);
res = replaceVariables(res);
console.log(res);
var tokens = tokenize(res);
console.log(tokens);
var prefix = toPrefix(tokens);
console.log(prefix);
console.log(getLispString(prefix));
I just wrote it so there might be some problems in the style but I think that the idea is clear.
You can get the function body by using the .toString method. After that you can use regular expression to match the return statement
(/return\s+(.*?);/g).exec(funcStr)[1];
Note that here you must use semicolons for successful match! In the next step all variables are transformed to number values using the CONSTANTS object (I see that you have some parameters left so you may need little modifications here). After that the string is being tokenized, for easier parsing. In next step the infix expression is transformed into a prefix one. At the last step I build a string which will make the output looks like what you need (+ - plus, - - minus and so on).
Since I'm not sure you're able to get the method's body after having invoked it, here is an alternative solution:
var a = 42;
var b = 2;
function action(c) {
return a + 4 * b + c;
}
/**
* get the given func body
* after having replaced any available var from the given scope
* by its *real* value
*/
function getFunctionBody(func, scope) {
// get the method body
var body = func.toString().replace(/^.*?{\s*((.|[\r\n])*?)\s*}.*?$/igm, "$1");
var matches = body.match(/[a-z][a-z0-9]*/igm);
// for each potential var
for(var i=0; i<matches.length; i++) {
var potentialVar = matches[i];
var scopedValue = scope[potentialVar];
// if the given scope has the var defined
if(typeof scopedValue !== "undefined") {
// add "..." for strings
if(typeof scopedValue === "string") {
scopedValue = '"' + scopedValue + '"';
}
// replace the var by its scoped value
var regex = new RegExp("([^a-z0-9]+|^)" + potentialVar + "([^a-z0-9]+|$)", "igm");
var replacement = "$1" + scopedValue + "$2";
body = body.replace(regex, replacement);
}
}
return body;
}
// calling
var actionBody = getFunctionBody(action, this);
// log
alert(actionBody);
Prints:
return 42 + 4 * 2 + c;
DEMO
You would then have to implement your own function toLISP(body) or any function else you may need.
Note that it won't work for complex scoped variables such as var a = {foo: "bar"}.