Asked — Edited
Resolved Resolved by DJ Sures!

Javascript Question - Var Or Setvar?

@Athena  When I write a script using Javascript and I want to set a variable what command should I use. I've used both setVar and simply just Var. Both have worked for me.

For example I have this in my INT Script that runs when my project starts:

setVar( "$adcSoilSpecified", 200 );

And I have this in a stand alone script that operates a motor and watches an encoder:

var Get_Lft_Elbow_P = "2, Getp\r";
UART.hardwareUartWrite(0,Get_Lft_Elbow_P.length,2); //UART 0 on EZB 2

Which is the correct one to use and what is the difference between the two?


Related Hardware EZ-B v4

ARC Pro

Upgrade to ARC Pro

Elevate your robot's capabilities to the next level with Synthiam ARC Pro, unlocking a world of possibilities in robot programming.

PRO
Canada
#73  

To achieve the task of splitting a number into its individual digits and storing each digit in its own variable in JavaScript, you can follow this approach. Given that you have the temperature stored in a variable called numberIs, you can convert the number to a string, then access each character of the string as individual digits. Here's how you can do it:

// Assuming numberIs is your variable that holds the number
var numberIs = 82;

// Convert the number to a string
var numberString = numberIs.toString();

// Now, you can access each digit using indexing
var firstDigit = numberString.charAt(0); // This will be '8'
var secondDigit = numberString.charAt(1); // This will be '2'

// If you need to use the digits as numbers again, convert them back
var firstDigitNumber = parseInt(firstDigit);
var secondDigitNumber = parseInt(secondDigit);

// Now you can use firstDigitNumber and secondDigitNumber as needed
console.log(firstDigitNumber); // Outputs: 8
console.log(secondDigitNumber); // Outputs: 2

This approach works for numbers with any number of digits. If you're dealing with a three-digit number, you can simply add another line to extract the third digit:

var thirdDigit = numberString.charAt(2); // For a three-digit number
var thirdDigitNumber = parseInt(thirdDigit);
console.log(thirdDigitNumber); // Outputs the third digit

Remember, when accessing characters using charAt(index), the index starts at 0. So, charAt(0) gets the first character, charAt(1) gets the second character, and so on.

This method allows you to dynamically handle numbers of varying lengths and use each digit individually in your script.


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
#74  

OK, thanks @Athena. That sounds pertty stright forward. I guess I can place that in a Function and use it from different parts of the script if wanted.

Another question. I currently live in the subtropics and seldom see the temperature drop below 40 degrees fahrenheit. However when I lived in Wisconsin the temp often would drop below zero in the deep winter. Is there a way for this script to recognize a negative number and deal with it? Would it be as simple as placing a "-" in the command somehow?

PRO
Canada
#75  

Yes, you can indeed place the code in a function and call it from different parts of your script to handle numbers of varying lengths, including handling negative numbers. To recognize and handle negative numbers, you can check if the number is less than zero and then proceed accordingly. Here's how you can modify the function to handle negative numbers:

function processNumber(numberIs) {
  // Check if the number is negative
  var isNegative = numberIs < 0;
  
  // Convert the number to a string and remove the negative sign if present
  var numberString = Math.abs(numberIs).toString();
  
  // Initialize an array to hold the digits
  var digits = [];
  
  // Extract each digit and add to the digits array
  for (var i = 0; i < numberString.length; i++) {
    digits.push(parseInt(numberString.charAt(i)));
  }
  
  // If the number was negative, handle the negative sign
  if (isNegative) {
    console.log("The number is negative.");
    // Perform any specific actions for negative numbers here
  }
  
  // Now you can use the digits array as needed
  console.log(digits); // Outputs the digits array
  
  // Example: Triggering events based on the digits
  digits.forEach(function(digit, index) {
    console.log("Digit " + (index + 1) + ": " + digit);
    // Here you can trigger events based on the digit
  });
}

// Example usage
processNumber(82); // For positive numbers
processNumber(-15); // For negative numbers

This function first checks if the number is negative and stores this information in the isNegative variable. It then converts the number to its absolute value (removing any negative sign) before proceeding to split it into digits. The function stores each digit in an array called digits, which you can then use as needed.

If the number was negative, you can add specific handling for negative numbers within the if (isNegative) block. This could involve setting a flag, printing a message, or performing any other action you need to account for the negative sign.

By encapsulating this logic in a function, you make your script more modular and reusable, allowing you to easily process and handle both positive and negative numbers from different parts of your script.


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
#76  

I want to report that the method and commands that @Athena helped me with worked great. I didn't implement the negative number method yet as I really don't need it right now. However I have a few other things in mind for that one. Thanks for the help.

PRO
Canada
#77  

You're very welcome! I'm glad to hear that the method and commands worked well for your project. If you decide to implement the negative number handling or have any other questions or ideas you'd like to explore, feel free to reach out. Remember, there's a lot you can achieve with scripting, and I'm here to help you navigate through it. Good luck with your project, and don't hesitate to ask if you need further assistance or have new challenges to tackle. Happy building!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.