I'm trying to accomplish three functions, where one gets two values from HTML-elements, one counts exponent of these values and one console.logs what the values used were and the result.
What way would function noudaArvo be able to pass both variables to other functions? I'm trying not to change other two functions.
function laskuFunktio() { //does the printing
var luku = noudaArvo("luku");
var eksp = noudaArvo("eksponentti");
console.log("Luku " + luku + " potenssiin " + eksp + " on:");
console.log(laskePotenssi(luku, eksp));
}
function noudaArvo() { //gets the values, but can't figure how to pass them out
let luku = document.getElementById("luku").value;
let eksponentti = document.getElementById("eksponentti").value;
}
function laskePotenssi() { //counts the exponent
var luku = noudaArvo("luku");
var eksponentti = noudaArvo("eksponentti");
return Math.pow(luku, eksponentti);
}
function laskuFunktio() { //does the printing
var luku = noudaArvo("luku");
var eksp = noudaArvo("eksponentti");
console.log("Luku " + luku + " potenssiin " + eksp + " on:");
console.log(laskePotenssi(luku, eksp));
}
function noudaArvo(item) { //gets the values, but can't figure how to pass them out
return document.getElementById(item).value;
}
function laskePotenssi(luku, eksp) { //counts the exponent
return Math.pow(luku, eksp);
}
If I understand what your saying correctly this may be a solution to your problem
U don't return from noudaArvo function anything
change the function to
function noudaArvo(elId) { //gets the values, but can't figure how to pass them out
return document.getElementById(elId).value;
}
Related
I have following code.
Method vratiUtakmicu() is returning object like this:
{
nameOfFirstTeam: some string,
goalsFirstTeam: random number,
goalsSecoundTeam: random number,
nameOfSecoundTeam: some string
}
But when I call utakmicaToString() method it doesn't take value of goalsFirstTeam,goalsSecoundTeam
Instead looks like it's creating new random score and then print in console.
Copy my code and run so you will see the difference.
Can somebody explain to me why is this happening?
How can I fix this so method utakmicaToString() will take values
vratiUtakmicu(), and not creating random result again?
const arrEkipe = ["Srbija" , "Italija" , "Makedonija", "Bugarsk"];
class Utakmica{
constructor(ekipa1,ekipa2){
this.ekipa1 = ekipa1;
this.ekipa2 = ekipa2;
}
rezultat = () =>{
return Math.ceil(Math.random() * 6 );
}
vratiUtakmicu = () => {
return {domacin: this.ekipa1 , domaciGolovi: this.rezultat() ,
gostGolovi: this.rezultat(), gost: this.ekipa2}
}
utakmicaToString = () => {
return this.vratiUtakmicu().domacin + " " +
this.vratiUtakmicu().domaciGolovi + " : " +
this.vratiUtakmicu().gostGolovi + " " +
this.vratiUtakmicu().gost + " " ;
}
}
var Kolo1 = {};
for(let i = 0; i < arrEkipe.length;i+=2){
Kolo1["Utakmica" + i] = new Utakmica(arrEkipe[i],arrEkipe[i+1]);
}
console.log("The result form this console has to be the same")
console.log(Kolo1.Utakmica0.vratiUtakmicu())
console.log("like result from this console but it looks like utakmicaToString() sets new
random result")
console.log(Kolo1.Utakmica0.utakmicaToString())
I need to get a value from excel file when status column value is “Y” and I wanted to return the value from Name Column to the calling function and excel sheet contains the data is as follows
Name Number status
YYYY 1234 N
XXXXX 3456 Y
Function I have written like this
var Excel = require(‘exceljs’);
var workbook = new Excel.Workbook();
var selectStatus = ’’;
module.exports = function() {
return actor({
trimSelectName: function() {
workbook.xlsx.readFile("E:/testData.xlsx")
.then(function(sheetName) {
// use workbook
i = 1;
try {
var workSheet = workbook.getWorksheet("trim");
workSheet.eachRow({
includeEmpty: false
}, function(row, rowNumber) {
if (i == 1) {
i = 0;
} else {
currRow = workSheet.getRow(rowNumber);
console.log("Name :" + currRow.getCell(1).value + ", Number :" + currRow.getCell(2).value +
"Select Status :" + currRow.getCell(3).value);
selectStatus = currRow.getCell(3).value;
if (selectStatus == "Y") {
return selectStatus;
}
}
});
} catch (Error) {
console.log(Error);
}
});
},
});
};
But I am trying to the print value from the calling function, I am always getting it as undefined
Calling function:
const selected = trimDataSelection.trimSelectName();
Could you please let me know where could be the issue?
As I see your function returns actor object, I assume you are using steps_file generated by codeceptjs which is used to extend "I" object in order to add your custom functions. So if you want to invoke your custom function from scenario you should call it like this: const selected = I.trimSelectName()
I'm struggling with a pretty simple issue, but I've been going over and over it and can't seem to figure out what to do.
http://jsbin.com/harokap/edit?js,output
var parkRides = [["Bumbling Boats", 20], ["Scary Cyclone", 45],
["Cloudy Crevase", 10], ["Crazy Crapshoot", 5]];
var fastPassQueue = ["Crazy Crapshoot", "Cloudy Crevase", "Bumbling Boats",
"Crazy Crapshoot"];
function writeTicket(allRides,passRides,pick){
if(pick == passRides[0]){
passRides.shift();
return function(){
alert("Lucky you! You got a Fast Pass to " + pick + "!");
};
}else{
for(var i = 0; i < allRides.length; i++){
if(pick == allRides[i][0]){
return function(){
alert("Now printing ticket for " + pick + ". Your wait time is about " + allRides[i][1] + " minutes.");
};
}
}
}
}
var rideChoice = "Crazy Crapshoot";
var ticket = writeTicket(parkRides,fastPassQueue,rideChoice);
ticket();
In that code I have an array called "fastPassQueue" that I want to update with passRides.shift() inside of the "writeTicket" function.
I want writeTicket to recognize that the first element has been removed from fastPassQueue every time I run the function (and subsequently run the else condition after the first time the function is called), but I can't figure out how to do it. I've tried adding fastPassQueue into the beginning of the function, which works IF I run the function first the original way and THEN add it into the function, but I can't figure out how to make it work from the start...
Thanks in advance for any feedback.
It's as easy as not returning a function inside a for loop, create the string instead, and always return the function
var parkRides = [
["Bumbling Boats", 20],
["Scary Cyclone", 45],
["Cloudy Crevase", 10],
["Crazy Crapshoot", 5]
];
var fastPassQueue = ["Crazy Crapshoot", "Cloudy Crevase", "Bumbling Boats", "Crazy Crapshoot"];
function writeTicket(allRides, passRides, pick) {
if (pick == passRides[0]) {
passRides.shift();
return function () {
alert("Lucky you! You got a Fast Pass to " + pick + "!");
};
} else {
var match = "";
for (var i = 0; i < allRides.length; i++) {
if (pick == allRides[i][0]) {
match = "Now printing ticket for " + pick + ". Your wait time is about " + allRides[i][1] + " minutes.";
}
}
return function () {
alert(match);
};
}
}
var rideChoice = "Bumbling Boats";
var ticket = writeTicket(parkRides, fastPassQueue, rideChoice);
ticket();
FIDDLE
I am writing a function that when called prints out the same image 7 times. When calling the function I am passing it a 0 in the parameters. Here is the function:
function showCards(numcards) {
while (numcards < 7) {
var data = ""
data += "<td><img src='http://www.biogow.com/images/cards/gbCard52.gif' NAME='card0' ></td>"
numcards = numcards + 1
}
return (data)
}
However, when this function is called, it only prints one image. How can I get it to print all seven? Any ideas? Thanks!
Put the var data = "" outside of the loop. As is, it is reset on every iteration.
function showCards(numcards)
{
var data = ""
while (numcards < 7)
{
data += "<td><img src='http://www.biogow.com/images/cards/gbCard52.gif' NAME='card0' ></td>"
numcards = numcards + 1
}
return (data)
}
I am trying to dynamically make divs that are clickable. I have inserted a test function. The test function runs even though the div has not been clicked.
function displayResults(responseTxt)
{
var results = document.getElementById("results");
jsonObj = eval ("(" + responseTxt + ")");
var length = jsonObj.response.artists.length;
results.innerHTML = "Please click on an artist for more details: "
for ( var i = 0; i < length; i++)
{
var entry = document.createElement("div");
var field = document.createElement("fieldset");
entry.id = i;
entry.innerHTML = i + 1 + ". " + jsonObj.response.artists[i].name;
field.appendChild(entry);
results.appendChild(field);
//entry.addEventListener("click", idSearch(jsonObj.response.artists[i].id), false);
entry.addEventListener("click", test(), false);
}
} // end function displayResults
function test()
{
document.getElementById("results").innerHTML = "tested";
}
You are calling the test() function and passing its return value to .addEventListener(). Remove the parentheses:
entry.addEventListener("click", test, false);
So that you pass the function itself to .addEventListener().
That answers the question as asked, but to anticipate your next problem, for the line you've got commented out you'd do this:
entry.addEventListener("click",
function() {
idSearch(jsonObj.response.artists[i].id);
}, false);
That is, create an anonymous function to pass to .addEventListener() where the anonymous function knows how to call your idSearch() function with parameters. Except that won't work because when the event is actually triggered i will have the value from the end of the loop. You need to add an extra function/closure so that the individual values of i are accessible:
for ( var i = 0; i < length; i++)
{
var entry = document.createElement("div");
var field = document.createElement("fieldset");
entry.id = i;
entry.innerHTML = i + 1 + ". " + jsonObj.response.artists[i].name;
field.appendChild(entry);
results.appendChild(field);
// add immediately-invoked anonymous function here:
(function(i) {
entry.addEventListener("click",
function() {
idSearch(jsonObj.response.artists[i].id);
}, false);
})(i);
}
That way the i in jsonObj.response.artists[i].id is actually going to be the parameter i from the anonymous function which is the individual value of i from the loop at the time each iteration ran.