How can I convert this PHP code to JavaScript?
$string = "string How Long is a Piece of String?";
if (strlen($string) < 5)
{
echo "string is less than 5";
}
else
{
echo "string is more than 5";
}
I assume this is just a really convoluted, badly explained, typo-ridden way of asking how to get the length of a string in JavaScript. If that's the case, just check the length property of the string, like this:
var str = 'How long is a piece of string?';
if (str.length < 5) {
alert('String is less than 5');
} else {
alert('String is greater than or equal to 5');
}
If this is not what you're asking, you really need to clarify the question.
Related
I have this array which links numbers to letters at the moment like this:
1-26 = A-Z
But there is more, 27=AA and 28=AB etc...
so basically when I do this:
var_dump($array[2]); //shows B
var_dump($array[29]); //shows AC
Now this array I made myself but it's becoming way too long. Is there a way to actually get this going on till lets say 32? I know there is chr but I dont think I can use this.
Is there an easier way to actually get this without using this way too long of an array?
It's slower calculating it this way, but you can take advantage of the fact that PHP lets you increment letters in the same way as numbers, Perl style:
function excelColumnRange($number) {
$character = 'A';
while ($number > 1) {
++$character;
--$number;
}
return $character;
}
var_dump(excelColumnRange(2));
var_dump(excelColumnRange(29));
here is the code which you are looking for :
<?php
$start = "A";
$max = 50;
$result = array();
for($i=1; $i<=$max; $i++) {
$result[$i] = $start++;
}
print_r($result);
?>
Ref: http://www.xpertdeveloper.com/2011/01/php-strings-unusual-behaviour/
This should work for you:
Even without any loops. First I calculate how many times the alphabet (26) goes into the number. With this I define how many times it has to str_repleat() A. Then I simply subtract this number and calculate the number in the alphabet with the number which is left.
<?php
function numberToLetter($number) {
$fullSets = (($num = floor(($number-1) / 26)) < 0 ? 0 : $num);
return str_repeat("A", $fullSets) . (($v = ($number-$fullSets*26)) > 0 ? chr($v+64) : "");
}
echo numberToLetter(53);
?>
output:
AAA
I realize this is extremely simple but I feel I'm over-looking something.
What I want to screen to display is
RED This is 0.
or
GREEN This is 1.
And for it to alternate back and forth between the displayed text. My logic if fine for alternating between Red and Green, but the "This is 0" and "This is 1" text is not displaying.
Here is my code so far:
<?php
$array = array(0=>"RED",1=>"GREEN");
$a_count = 0;
$count = 0;
while($count<10)
// DO 9 TIMES
{
echo $array[$a_count] . ' ';
//SUDO FOR IMAGE BEING DISPLAYED
while($array[$a_count] == 0)
{
echo "This is 0.<br>";
}
while($array[$a_count] == 1)
{
echo "This is 1<br>";
}
//<----SWITCH BACK AND FORTH---->
if($a_count == 1)
{
$a_count = 0;
}
else
{
$a_count++;
}
//<----------------------------->
$count++;
}
?>
I realize the easiest way to get what I would like is:
<?php
$array = array(0=>"RED",1=>"GREEN");
$a_count = 0;
$count = 0;
while($count<10)
// DO 9 TIMES
{
echo $array[$a_count] . ' ';
//SUDO FOR IMAGE BEING DISPLAYED
//<----SWITCH BACK AND FORTH---->
if($a_count == 1)
{
echo "This is 1<br>";
$a_count = 0;
}
else
{
echo "This is 0.<br>";
$a_count++;
}
//<----------------------------->
$count++;
}
?>
But this code does not contain the logic I need for the continuation of this project.
I would greatly appreciate an answer as to why my first code is not printing "This is 0."
Thank you!
What you are looking for is a mod count.
Change your loop for:
for($i=0;$i<10;$i++){
echo $array[$i%2].' This is '.($i%2);
}
The modulo operator returns the remainder of a division.
See: What are the practical uses of modulus (%) in programming?
Why not something like this:
$colors = array(0 => 'Red', 1 => 'Green');
$idx = 0;
$count = 0;
while($count < 10) {
echo "The color is {$colors['$idx']}<br />";
$count = 1 - $count; // if $count is 1, it becomes 0. if it's 0, it becomes 1
}
Your while() loops are basically totally useless. You're trying to compare the RED and GREEN strings again 0. If either evaluation happens to be true, you'll end up with an infinite loop.
Ignoring the inefficiency of this script, your while loops are comparing against the array's values, not its keys. But fixing this problem will actually expose another - an infinite loop.
You aren't changing the value of $a_count in your while loops, so there is no way for them to end.
The problem is here:
while($array[$a_count] == 0)
{
echo "This is 0.<br>";
}
while($array[$a_count] == 1)
{
echo "This is 1<br>";
}
Once it enters the first loop, it will just keep echoing "This is 0.<br>", as $a_count is unchanging.
It looks like you could change these whiles to ifs to make your code work the way you want. You also probably want to check that $a_count is 0 or 1, rather than $array[$a_count]
Well, in your first example before the while($count<10), have you initilize your values?
If you do, you must have a display like that :
RED This is 0.0
This is 0.0
This is 0.0
This is 0.0
This is 0.0
...
"This is 0.0" is display in a infinite loop.
while($array[$a_count] == 0)
{
echo "This is 0.$count<br>";
}
while($array[$a_count] == 1)
{
echo "This is 1<br>";
}
You must change the value in a while loop.
Other tips, I think you mush take a look at "foreach" php loop. Can be useful for what you want to do. modulos can also help you.
I think probably the easiest way to do this is to just use a switch statement. I feel really silly for not thinking of it before.
while($count<10)
{
echo $array[$a_count] . ' ';
//PSUEDO FOR IMAGE BEING DISPLAYED
switch($a_count):
{
case 1:
echo "This is RED.<br>";
$a_count = 0;
break;
case 0:
echo "This is GREEN.<br>";
$a_count++;
break;
}
$count++;
}
What is wrong with this if-else statement.
if((strlen($objectData['pss'] >= 8))AND(strlen($objectData['pss'] <= 20)))
{
//do my bidding
}
else
{
echo "String to short or to long";
}
Ultimately I am trying to find if the variable is greater than or equal to 8 chars while being under or equal to 20 chars. My test string is 11 char, and I am seeing string to short/to long. I've done similar to this in the past, so I dunno what I mucked up at the moment (maybe Im just to tired to realize it)
if (strlen($objectData['pss']) >= 8 && strlen($objectData['pss']) <= 20)
if ((strlen($objectData['pss']) >= 8) and (strlen($objectData['pss']) <= 20))
{
//do my bidding
}
else
{
echo "String to short or to long";
}
I have corrected you brackets
Yes you are indeed "to tired".. You are basically counting the length of an expression instead of the string itself:
if((strlen($objectData['pss']) >= 8)AND(strlen($objectData['pss']) <= 20))
what am I doing wrong here guys?
$string = "string How Long is a Piece of String?";
if $string = <5;
{
echo "string is less than 5";
}
else
{
echo "string is more than 5";
}
1st, condition are in parenthesis.
2nd, you don't need a ; after a condition.
3rd, less than is simply < not <= unless you want to echo "string is less or equals than 5"
$string = "string How Long is a Piece of String?";
if (strlen($string) < 5)
{
echo "string is less than 5";
}
else
{
echo "string is more than 5";
}
Others pointed out the syntax errors, to actually compare to the length of the string you need to use the strlen function:
$string = "string How Long is a Piece of String?";
if (strlen($string) < 5)
{
echo "string is less than 5";
}
else
{
echo "string is more than 5";
}
Type juggling it is called:
http://nl2.php.net/manual/en/language.types.type-juggling.php
$string = "string How Long is a Piece of String?";
if ($string < 5)
string is cast to int, becomes 0
if (0 < 5)
true!
strlen / mbstrlen are possible candidates you're loking for
But that wasn't the question, there are obivously more things wrong with the code :)
may be you're looking for the strlen() function?
missing parentheses around if statement and no need for semi-colon? also less than or equal operator in wrong order. should be like this:
if ($string <=5)
{
echo "string is less than 5";
}
Also note that if that string has multi byte chars it will return a wrong char count, but a byte count.
You'll probably need to know this down the track :) For now, get on top of your syntax.
I need help with performing a binary search with a search term ($searchTerm) and comparing it to a dictionary ($dictionary).
Basically, it reads a dictionary file into an array. The user inputs some words, that string becomes $checkMe. I do an explode function and it turns into $explodedCheckMe. I pass each term in $checkMe to binarySearch as $searchTerm (Okay, my code is confusing). I think my logic is sound, but my syntax isn't ...
I've been using this a lot: http://us3.php.net/manual/en/function.strcasecmp.php
here is my code: paste2.org/p/457232
I know this doesn't directly answer your question, but have you considered using pspell and a custom dictionary?
So you are looking up exact strings in the dictionary. Why don't you a simple array for this? The native PHP's hash table is definitely going to be faster than a binary search implemented in PHP.
while (!feof($file)) {
$dictionary[strtolower(fgets($file))] = 1;
}
...
function search($searchTerm, $dictionary) {
if ($dictionary[strtolower($searchTerm)]) {
// do something
}
}
But if you really want to use a binary search, try this:
function binarySearch($searchTerm, $dictionary) {
$minVal = 0;
$maxVal = count($dictionary);
while ($minVal < $maxVal) {
$guess = intval($minVal + ($maxVal - $minVal) / 2);
$result = strcasecmp($dictionary[$guess], $searchTerm);
if ($result == 0) {
echo "FOUND";
return;
}
elseif ($result < 0) {
$minVal = $guess + 1;
}
else {
$maxVal = $guess;
}
}
}
The main problem was that you can't set $maxval to $guess - 1. See the wikipedia article on binary search, it's really good.