Adobe Javascript Calculation Errors - javascript

I want to check fields "Code1 to Code 32" for anything starting with "A" and ending with "B". I want the count to output to the field "Total1". I input the code under properties of "Total1" in the custom calculation. The Javascript doesn't calculate. What am I doing wrong?
var total = 0;
for (var i = 1; i <= 32; i++) {
var f = this.getField("Code" + i).valueAsString;
var v = f.valueAsString;
if (v.charAt(0) == "A" && v.charAt(v.length - 1) == "B") total++;
}
event.value = total;

Related

How to swap first and last digits of a number using for loop in Javascript

I need to ask user to input a 3 digit number and then swap the first and last numbers using a for loop. This is what I have so far, but I'm stuck. Using a for loop seems illogical, but that is what I have to do:
num = prompt("Please input a number with 3 digits.");
let firstDigit = num[0];
let secondDigit = num[1];
let lastDigit = num[2];
for (firstDigit < 10; secondDigit < 10; lastDigit < 10); {
console.log(lastDigit + secondDigit + firstDigit);
}
Please help!
Thanks
is it help for you?
// let num = prompt("Please input a number with 3 digits.");
// if (num.length > 3) alert("3 digits please");
// else {
// let answer = "";
// for (var i = 2; i >= 0; i--) {
// answer = answer + num[i];
// }
// console.log(answer);
//}
let num = prompt("Please input a number");
let temp = "";
let answer = "";
for(let i = 0 ; i < num.length; i++) {
if (i === 0) temp = num[i]; // save first number to temp
else if (i === num.length - 1) {
// When the last number is encountered, the last number is put at the beginning, and the first number stored in temp is put at the end.
answer = answer + temp;
answer = num[i] + answer;
}
else answer = answer + num[i];
}
console.log(answer);

JavaScript: for Loop inside function stops based on user input

I'm trying to write a javascript code where I can have the user input a number to count the loop that many times.
The loop is supposed to run the Fibonacci numbers in order as many times as specified and stop. I was asked to have the loop inside a function.
Image of the code
< !DOCTYPE HTML >
<
html >
<
title > Fibonacci < /title> <
head >
<
script >
function chkInput() {
var num1 = document.getElementById = ("userInput").value;
var outputString = "" + (i);
var a, b, r;
a = 0;
b = 1;
r = b;
for (var i = 1; i <= num1; i++) {
outputString += "</br>" + (r * num1);
r = a + b;
a = b;
b = r;
}
document.write(outputString);
}
<
/script> <
/head> <
body >
<
input type = "text"
id = "userInput" >
<
input type = "button"
id = "btn"
value = "Enter"
onclick = "chkInput()" >
<
/body> <
/html>
Works well... where n is the number elements
function chkInput(){
var num1 = document.getElementByID =("userInput").value;
document.write(fibonacci_series(num1 ));
}
& then the Fibonacci series as per this doc
var fibonacci_series = function (n)
{
if (n===1)
{
return [0, 1];
}
else
{
var s = fibonacci_series(n - 1);
s.push(s[s.length - 1] + s[s.length - 2]);
return s;
}
};
The array fibonacci_series contains the series... & return keyword here is like the loop stopper

I want to output the number that contain "7" as the LUCKY NUMBER and the number that can be divided evenly by 7 as LUCKY NUMBER

I tried this code. It works for number that can be divided evenly by 7 as Lucky Numbers. But it doesn't work for some numbers. I can't find where I made a mistake.
for(i=1; i<=1000; i++)
{
var x = 7;
var y = i;
var z = y % x ;
if ( z == 0 )
{
document.write("Lucky Number" +"<br>");
i++;
}
var number = i;
var num = number.toString();
var matchedposition = num.search(["7"]);
if ( matchedposition == true )
{
document.write("Lucky Number" +"<br>");
i++;
}
document.write(i+"<br>");
}
You can try like this.
for(i=1; i<=1000; i++)
{
var z = i % 7 ;
if (( z == 0 ) &&( i.toString().search(7)))
{
console.log("Lucky Number"+ i +"<br>");
}
}
So here in this code i did two things
1) I am not incrementing 'i' after every If . Because if you increment i after every IF condition then you will not check all numbers from 1 to 1000 . So whenever yours condition will be true u increase i by one and then again i will increment by loop. so you are escaping one number to be checked.
2) I combine both condition together to reduce code.
I hope it will help you
Try this not sure:
for(i=1; i<=1000; i++)
{
var x = 7;
var y = i;
var z = y % x ;
var isFound=false;
if ( z == 0 )
{
document.write("Lucky Number" +"<br>");
isFound =true;
}
var number = i;
var num = number.toString();
var matchedposition = num.search("7");
if ( matchedposition > 0 && !isFound)
{
document.write("Lucky Number" +"<br>");
}
document.write(i+"<br>");
}

how can i compare the first and last / second and second last elements of the string in java script

how can i compare the first and last / second and second last elements of the string in java script
ex: we take in 102201
compare 1 and 1
0 and 0
2 and 2
it's want to any string.
if another ex: element
then
check e and t
l and n
e and e
m forgot it.
my code like
function check() {
var input = document.getElementById("check").value;
var inputcount = "";
if ((input.length) % 2 === 0) {
inputcount = (input.length) / 2;
} else {
inputcount = (input.length - 1) / 2;
}
for (var x = 0; x < inputcount; x++) {
//....
}
}
You can subtract the x from the array length to get the last character
function check() {
var input = document.getElementById("check").value;
var inputcount = Math.floor(input.length / 2);
for (var x = 0; x < inputcount; x++) {
snippet.log(input[x] + ' and ' + input[input.length - x - 1])
}
}
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<input id="check" onchange="check()" />

Combining two simple scripts

How can I modify the script below so that it alerts not only when the field is empty, but when the field is empty or contains less than 10 digits (it can contain anything, not only digits)?
if (myform.mytextinput.value=="")
alert ('Please enter something!");
A good script that checks for ten digits is this:
var input_dg = document.getElementById("mytextinput");
var text_dg = input_dg.value;
var totalNrOfDigits = 0;
for(var i = 0; i < text_dg.length; i++){
if(/\d/.test(text_dg[i])){
totalNrOfDigits++;
}
}
alert ('Please enter at least 10 digits!');
I just need to "paste" the second script in the first, but I lack the know-how. Both scripts are inside a large form validation script...
Just use ||, the OR operator:
if (myform.mytextinput.value=="" || myform.mytextinput.length < 10)
To count the number of digits in a string I recommend this code (From this SO answer):
var totalNrOfDigits = myform.mytextinput.replace(/[^0-9]/g,"").length;
// And now combine both checks:
if (myform.mytextinput.value=="" || totalNrOfDigits < 10){
// Alert the user
}
If you want to use the same code you were using you only need to replace the first part:
var input_dg = document.getElementById("mytextinput");
var text_dg = input_dg.value;
var totalNrOfDigits = 0;
for(var i = 0; i < text_dg.length; i++){
if(/\d/.test(text_dg[i])){
totalNrOfDigits++;
}
}
if (myform.mytextinput.value=="" || totalNrOfDigits < 10){
// Alert the user
}
Try this using the OR(||):
if (myform.mytextinput.value=="" || myform.mytextinput.length < 10)
You can count them with a regex
var str ='1a2b3',
digits = (str).match(/\d+/g); // returns ["1", "2", "3"]
if ((! str.length) || (! digits) || (digits.length < 10)) {
alert ('Please enter at least 10 digits!');
}
Did you want this?
var input_dg = document.getElementById("mytextinput");
var text_dg = input_dg.value;
if(text_dg == "") {
alert ('Please enter something!');
} else if(text_dg.length < 10) {
alert("Please enter at least 10 digits!");
}
Fiddle.

Categories

Resources