Move a node to a certain position withou dragging

I use D3.js and i want that a node move to a certain position ( 1; 1000) after i clicked a button.

The onclick function looks like this:

  .on("click", function(d) {
        nodes[1].x = 1;
        nodes[1].y = 1000;
        nodes[1].px = 1;
        nodes[1].py = 1000;
        nodes[1].fixed = true; };

My problem is that this does not work right. Can someone help me maybe?

Thanks a lot!

Source: stackoverflow-javascript

Javascript function. What is missing?

Write a program to find count of the most frequent item of an array.

Assume that input is array of integers.


input array: [3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3]
ouptut: 5
Most frequent number in example array is -1. It occurs 5 times in input array.

Here is my code:
function mostFrequentItemCount(collection) {

var copy = collection.slice(0);

for (var i = 0; i < collection.length; i++) {
 var output = 0;
 for (var x = 0; x < copy.length; x++) {
   if (collection[i] == copy[x]) {
  return output; 

It seems to be just counting the reoccurrence of the first number in the array not the one that occurs the most. I can’t figure out how to make it count the most occurring one.

Source: stackoverflow-javascript

How to wrap 2 functions into one call

Ok, so I’m working on a WordPress project (PHP) using Advanced Custom Fields and a little bit of PHP. I’ve created 2 functions that will create a container div around some text:


 <?php container_start(); ?>

   Text goes here

 <?php container_end(); ?>


This generates the following code:


Text goes here

This is great as it works as expected. What is going behind the scenes are these 2 functions:

function container_start() {

$container = get_sub_field('container'); 

if ($container): 
echo '
'; endif; } function container_end() { $container = get_sub_field('container'); if ($container): echo '
'; endif; }

Question is: Is there a way to optimize how is this accomplished? I find it not too practical having to call 2 functions just to add and then close a div. Is there a way I could wrap this into just one call?

Source: stackoverflow-php

Creating a Bracketed Sales Commission Calculator with Javascript

I’m trying to create a calculator using javascript syntax for a realtor client. She wants to use it to show her clients what her commission amount would be. Here’s the parameters she gave me. They are bracketed (like income taxes):

  • 0.575% up to $100,000
  • any amount in excess of $100k, up to $1m is charged at 0.5%
  • any amount in excess of $1m is charged at 0.25%


  • The first $100k is $575
  • The first $1m is $5075 ($575 for the $100k plus $4500 for the next $900k)

Here is my code, where the input is x.

    if(x <= 100000) return x*0.00575;
    if(x > 100000 && < 1000000) return ((x-100000)*0.005+575);
    if(x >= 1000000) return ((x-1000000)*0.0025+5075);

Is this correct? What have I done wrong?

Source: stackoverflow-javascript

Having trouble getting a counter reset function to work using sockets in express

I have a project I am working on for a coding bootcamp. For the project I need to create a website that has a button that when pressed adds +1 to a counter and displays how many times the button has been pressed. The project also needs a reset button that when pressed resets the count back to zero.

I created the project and everything is working well except for the reset function. Whenever I press the reset button nothing happens. I have looked over the code and I don’t see any errors in the reset function that I wrote. Could anyone out there give me any tips on something I may be overlooking as a newbie coder?

Here is my index.js page:

module.exports = function Route (app, server) {
    // this gets the module (below)
    var io = require("").listen(server)

    var counter = 0; //saves the count

    io.sockets.on("connection", function(socket){
        socket.on("push_button", function(data){
            counter +=1;
            io.emit("push_counter", {response: counter}); 
            /* io.emit is a full broadcast, thus sends data to all connectd clients
            will send counter number to index.ejs after increasing by 1 */

        socket.on("reset_counter", function(data){
            counter = 0;
            io.emit("reset_response", {response: counter});
            /* Will set counter to 0 and send to index.ejs */


    // root route to render the index.ejs view (below)
    app.get("/", function(request, response){

Here is my index.ejs file that I have stored in a views folder. This page contains the reset function:

<html lang ="en US">
    <meta charset="UTF-8">
    <title>Epic Button Game</title>
        $(document).ready(function (){
           // alert("jquery loaded");
           var socket = io.connect();

          //lets emit an event when somebody pushes the button
                socket.emit("push_button", {action: "Somebody pushed the button"});

          // this event will emit to the server to reset the counter into 0
                socket.emit("reset_counter", {action: 'Resetting the counter'});

          // listen to the push_counter event from server
          socket.on("push_counter", function (data){
             console.log(data); // 
        <h2 id="message">Reset needs to be fixed!</h2>
        <h4>Push the button to update the count!</h4>
        <button id="push">Push this awesome button</button>
        <button id="reset">Reset Count</button>

Here is my server.js page:

// require express, path, body-parser
var express = require("express");
var path = require("path");
var bodyParser = require('body-parser');
// create the express app
var app = express();

// static content
app.use(express.static(path.join(__dirname, "./static")));
// setting up ejs and our views folder
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'ejs');
// tell the express app to listen on port 3000
var server = app.listen(3000, function() {
    console.log("listening on port 3000");

//we're going to have /routes/index.js handle all of our routing
var route = require('./routes/index.js')(app, server);

Finally my package.json page which contains a list of dependencies I am using:

  "name": "epic_button_game",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "node server.js"
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ejs": "^2.5.5",
    "express": "^4.14.1",
    "": "^1.7.2"

Thanks for in advance for any suggestions.

Source: stackoverflow-javascript