Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Below is the code in Javascript:

const printNumberStarMatrix = function (row, col) {

    for (let i = 1; i <= row; ++i) {
        let strToPrint = "";
        for (let j = col - i; j >= 1; --j) {
            strToPrint = j + strToPrint;
        }
        while (strToPrint.length < col) {
            strToPrint += "*";
        }
        console.log(strToPrint);
    }
}


What I have tried:

Below is the code I have written in php:

<?php
$printNumberStarMatrix = function ($row, $col) {
	for ($i = 1; $i <= $row; ++$i) {
		$strToPrint = "";
			for ($j = $col - $i; $j >= 1; --$j) {
				$strToPrint = $j + $strToPrint;
			}
	}
	while ($strToPrint < $col) {
		$strToPrint += "*";
		}
echo $strToPrint;
	};
$printNumberStarMatrix(5, 6);
?>
Posted
Updated 27-Nov-18 23:03pm
v2
Comments
jsc42 28-Nov-18 3:43am    
Look closely at your transliteration, you've missed something (clue: it is in the 'while' clause).
Member 12598972 28-Nov-18 3:58am    
I used count($strToPrint) and strlen($strToPrint), but it still shows "A non-numeric value encountered on line 6.
A non-numeric value encountered on line 10"

1 solution

Do these two lines do exactly the same thing?
JavaScript
while (strToPrint.length < col)
PHP
while ($strToPrint < $col)
 
Share this answer
 
Comments
Member 12598972 28-Nov-18 3:59am    
I used count($strToPrint) and strlen($strToPrint), but it still shows "A non-numeric value encountered on line 6.
A non-numeric value encountered on line 10"
OriginalGriff 28-Nov-18 4:08am    
You do realize that you are spending more time and effort in trying not to wrote your own code that you would if you did your homework yourself?
This is an elementary exercise, designed to get you started and thinking about "how to code" - and if you "cheat" and just find something you think may work in a language you don't even know, then you personally don't get the benefit of the exercise. It's really a good idea to do this yourself as it helps you understand how you need to thi8nk for later, more complex tasks.

Give it a try! It's really not that difficult job to do - heck it's probably just a dozen lines of code!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900