2

I'm currently working on a little project which use pug (I discovered it just yesterday).

Here is my configuration:

file1.pug

- let resultArray = oldArray;

input(
  type='button'
  name='resultButton'
  placeholder='Click me...'
  onclick="addNumber(resultArray)"
 )

script
  include scripts/search_script.js

externalScript.js

function getTotal(resultArray) {

  let result = 0;
  let newArray = [];

  for(let i = 0; i < resultArray.length; i++){
    result += resultArray[i]; 
  }  

  newArray.push(result);

  return newArray;
}

It's just a little (weird) example of what I did.

The thing is, I was wondering if the value of resultArray which is in my file1.pug can be replaced by the value returned from the method getTotal() in my externalScript.js?

1

1 Answer 1

2

Pug won't have access to javascript included inside a script tag, which will only run in browser. If you want to use the getTotal() function in Pug, you could include it in an unbuffered code block in your Pug file like this:

-
  function getTotal(resultArray) {

    let result = 0;
    let newArray = [];

    for (let i = 0; i < resultArray.length; i++) {
      result += resultArray[i]; 
    }

    newArray.push(result);

    return newArray;
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.