Adding 3 drop down text values in jscalc.io - javascript

I created a simple calc with jscalc.io and I'm having trouble parsing values so I can add them.
I have played with a few variations of the following code but no luck. My javascript knowledge is really limited, like this is the first time writing something in JS. :)
jscalc.io returned the following error for this code:TypeError: null is not an object (evaluating 'inputs.Age.value')
"'use strict';
var varAge = parseFloat(inputs.Age.value);
var varWeight = parseFloat(inputs.Weight.value);
var varGender = parseFloat(inputs.Gender.value);
return {
total: varAge + varWeight + varGender
};
Any help would be much appreciated, thanks!

Just going to preface this by saying that I've never used jscalc.io, but I looked at the example and it looks like you're supposed to use inputs.Age instead of inputs.Age.value. Also, if you make your inputs as Numbers, you don't need to use parseFloat. So your fixed code would look like this:
'use strict';
var varAge = inputs.Age;
var varWeight = inputs.Weight;
var varGender = inputs.Gender;
return {
total: varAge + varWeight + varGender
};

Related

Tau-Prolog Interpreter shows different behaviour than SWI-Prolog

I'm currently trying to implement some basic Prolog queries in Tau-Prolog. Although I have working queries in SWI-Prolog, I can't implement them to work in Tau-Prolog.
I would like to return the name of all Robots that are in the database and have the Interface "B".
Is there something important I am missing here? I think that sub_string/5 might be the reason why it's not working. It also won't work when I paste the Code into the trial interpreter on http://tau-prolog.org/
Does anyone know a way to fix this query so it could work in Tau-Prolog? Thanks in advance!
<script>
var session = pl.create(1000)
var database = `
robot('Roboter1','A', 1, 550).
robot('Roboter2','C', 2, 340).
robot('Roboter3','B', 2, 430).
robot('Roboter4','A', 2, 200).
robot('Roboter5','B', 3, 260).
`
function start_query_RwB(){
query_RwB();
}
function query_RwB(){
var queryRwB = "queryRwB :-write('Interface B has: '),robot(Name, Interface,_,_),sub_string(Interface,_,_,_,'B'),write(Name),nl, fail."
var code_pl = database.concat(queryRwB);
var parsed = session.consult(code_pl)
var query = session.query('queryRwB.')
function inform(msg) {
show_result4.innerHTML += msg
}
session.current_output.stream.put = inform;
var callback = function(answer) {
}
session.answer(callback);
}
</script>
Use sub_atom/5 instead of sub_string/5 in the definition of the queryRwB variable as you use atoms, not strings, in the definition of the predicate robot/4:
var queryRwB = "queryRwB :-write('Interface B has: '),robot(Name, Interface,_,_), sub_atom(Interface,_,_,_,'B'),write(Name),nl, fail."
Note that sub_atom/5 is a standard predicate (that's implemented by Tau Prolog) while sub_string/5 is a proprietary predicate only found in some Prolog systems like ECLiPSe and SWI-Prolog.

setting object through variable containing strings not working

I have developed a pretty basic audio player on on my website similar to SoundClouds.
I have managed to successfully finish it, and I am starting to clean up all the markup, including (trying) removing all inline event handlers (i.e: onclick="" or onkeyup="").
However, I have come across a bit of a problem in my JavaScript whilst doing this.
It's a bit hard to explain so I'm going to post my JavaScript, then give you an idea of what the problem is:
$(".track-w__trigger").click(function(){
var tid = $(this).attr('aria-trackid'); // aria-trackid is equal to a random integer created by PHP
var tiW = 'w' + tid + 'w'; // this is an object (i.e: w3w)
playPauseButton(this, tiW, ti);
});
function playPauseButton(button, wave, trackID){
var button = $(button);
if(wave.isPlaying()){ // the object (w3w.isPlaying())
button.removeClass("playing");
wave.pause();
} else {
button.addClass("playing");
wave.play();
}
}
What I used to have was
<div class="track-w__trigger" onclick="playPauseButton(this, w3w, 3)" id="w3w-trigger"></div>
and now it is:
<div class="track-w__trigger" aria-trackid="3" id="w3w-trigger"></div>
As you can see in the second block of code. w3w is an object. However, because I have set it using my click function in a separate script using string quotes. JavaScript has gone ahead and made it a string.
So now when I use wave.isPlaying() for example; it does nothing.
I have no idea how to fix this, and no result on Google will help me. Any help in fixing this issue would be greatly appreciated,
Thanks!
EDIT:
This is where & how w3w is set:
var w3w = Uki.start({
// e.t.c
});
Use eval
wave = eval(wave);
to evaluate the string as a function
or use a safer way
wave = window[wave];
https://jsfiddle.net/zmr412q7/
Instead of having each object as a seperate variable, create an object that contains each object at the id representing the N in wNw. Ex:
var wNw = {};
// represents w1w
wNw[1] = (Uki.start({
// e.t.c
}));
// represents w17w
wNw[17] = (Uki.start({
// e.t.c
}));
// represents w3w
wNw[3] = (Uki.start({
// e.t.c
}));
This gives you an object that looks like:
{
1: object_that_was_w1w
17: object_that_was_w17w
3: object_that_was_w13w
}
And then your click handler looks like this:
$(".track-w__trigger").click(function(){
var tid = $(this).attr('aria-trackid'); // aria-trackid is equal to an integer of 1 to 5
var tidNum = parseInt(tid);
playPauseButton(this, wNw[tidNum], ti);
});
You can try something like this,
var tid='w'+5+'w';
var tryout=tid.valueOf();
console.log("getting the object "+tryout)
The valueOf property converts the string to an object

how to use jade/pug conditionals inside a script block?

i'm struggling to get this working. im actually using Chart.js and trying to populate my graphs with arrays of data.
so my code looks like
block lower-scripts
script.
var data = {
labels : [
each visit in visitstats
#{visit.date},
],
the output i get looks like
<script>var data = {
labels : [
each visit in visitstats
04/17/2016,
],
and i dont think that is right. should it print the each statement out into the html output?
I've tried following a few questions but cant get this working.
It doesn't look like the each statement runs. tried using the - to make it run. tried the pipe in front of the js to make it the exception. nothing.
can anyone show me where i'm going wrong?
Put a hidden input tag in your body in jade e.g.
body
input(type="hidden", id="visit-stats-json", value= JSON.stringify(visitstats) )
script.
var visitStatsValue = document.getElementById("visit-stats-json");
var visitStatsJSON = JSON.parse( visitStatsValue );
var labelsArray = [];
for( var i = 0; i < visitStatsJSON.length; i++ )
{
var visit = visitStatsJSON[i];
labelsArray.push( visit.date );
}//for
var data = { labels: labelsArray };
Now your data variable should have the value you want.
Edit:
I use exactly the same syntax and it works. Please note that there is a space after value=. If it still doesn't work, you can also try following way to achieve the same result:
-var visitStatsString = JSON.stringify(visitstats);
input(type="hidden", id="visit-stats-json", value= visitStatsString )
Can use inline interpolation:
script.
var visitstats = #[JSON.stringify(visitstats)];
...

AngularJS is removing my $_parent property when the object is passed back to the controller function

I have a linked list of JavaScript objects that I want to generate breadcrumbs from.
Because I'm using the list in recursive directive I need to use $_prev and $_next (instead of prev and next) so I won't get JSON.parse error about circular reference.
var myapp = angular.module('myApp', []);
var data00 = { id: 0, label: 'label_0' }
var data01 = { id: 1, label: 'label_1' }
var data02 = { id: 2, label: 'label_2' }
data00.$_next = data01;
data01.$_next = data02;
data01.$_prev = data00;
data02.$_prev = data01;
myapp.controller('testController', function($scope){
$scope.data = data02;
$scope.getBreadCrumbs = function(branch){
var crumbs = branch.label;
while(branch.$_prev){
branch = branch.$_prev;
crumbs = branch.label + ' \\ ' + crumbs;
}
return crumbs;
}
});
jsFiddle
Everything works in chrome, but in IE8 inside getBreadCrumbs function $_prev property does not exist.
Any input how I can debug the issue would be greatly appreciated.
I'm not sure about your IE8 problem but IMHO your approach is not very good. I believe that by using a better approach you will cut down future development/maintenance and quite possibly get rid of your IE8 bug. What I would do is put all your dataxx objects into a single array and then either use a function or a angular directive(s) to create your breadcrumbs from that array.
I strongly discourage you from doing something like this
data00.$_next = data01;
data01.$_next = data02;
data01.$_prev = data00;
data02.$_prev = data01;
Because as you add more data points it'll be more unnecessary work. You can either use the length of the array to see if you are on the first/last breadcrumb on or use the $first $last property that angular gives you on an ng-repeat

question regarding serializeArray and passing into url

I am not totally familiar with javascript, jquery.
I am trying to do the following. Note a-f are names for the dropdown menus. Can someone help clarify? thanks
var a_params = $("#a").serializeArray();
var b_params = $("#b").serializeArray();
var c_params = $("#c").serializeArray();
var d_params = $("#d").serializeArray();
var e_params = $("#e").serializeArray();
var f_params = $("#f").serializeArray();
params.push({ name: 'menu_mode', value: '2-1' });
$.get("./scripts/model.cgi", a_params,b_params,c_params,d_params,e_params,f_params, function(data){
$("#grapharea").html(data);
$("#prog").html(" ");
});
More Comments: in the cgi script i am dumping the inputs to see if i am receiving the values from the a-f_params but this isn't the case. Any ideas why?
You have to create 1 array(or jquery-object in this case) from all object's, and serialize this array.
$('#a,#b,#c,#d,#e,#f').serializeArray();
But this is only needed, if you dont want to serialize e.g. all input-fields.
Otherwise you can use simply
$('#form').serializeArray();

Categories