Guys I need your assistance I should accomplish several tasks given by supervisor to make triangle using for loop. But I can't get the exact one. Please help me with that!
Here is an illustration how the final output should look like:
Task 2
input
N (example: N = 5)
output
*
**
***
****
*****
Task 3
input
N (example: N = 5)
output
* * *
* *
* * *
* *
* * *
Task 4
input
N (example: N = 5)
output
*****
* *
* *
* *
*****
and this is my code:
function mixedTriangle (n) {
for (var i = 0; i < n; i++) {
for (var j = 0; j <= i; j++) {
document.write('*');
}
document.write('<br>');
}
}
mixedTriangle(5);
this is my take on it (scaling width with n):
first one you want to walk backwards on the columns so we start at the right side.
for the second function the we switch the pattern for each row other than that it's just using modulus to switch for each column
last one you just need the border which basically means minimum column or row or maximum column or row
<script type="text/javascript">
var fullSign = '*';
var emptySign = ' ';
var newRowSign = '<br>';
function mixedTriangle (n) {
for (var row = 1; row <= n; row++) {
for (var col = n; col > 0; col--) {
if(col <= row)
document.write(fullSign);
else
document.write(emptySign);
}
document.write('<br>');
}
}
function mixedTriangle2(n) {
for (var row = 0; row < n; row++) {
for (var col = 0; col < n; col++) {
if(row % 2 == 0) {
if(col % 2 == 0)
document.write(fullSign);
else
document.write(emptySign);
}
else {
if(col % 2 == 1)
document.write(fullSign);
else
document.write(emptySign);
}
}
document.write(newRowSign);
}
}
function mixedTriangle3(n) {
for (var row = 1; row <= n; row++) {
for (var col = 1; col <= n; col++) {
if(row == 1 || col == 1 || col == n || row == n)
document.write(fullSign);
else
document.write(emptySign);
}
document.write(newRowSign);
}
}
// mixedTriangle(5);
// mixedTriangle2(5);
mixedTriangle3(5);
</script>
//create function
function makeDownwardStairs(height) {
//initialize an empty array inside function.
let pyr = [];
//initialize loop and run a number of times
for (let i = 1; i <= height; i++) {
//update array w/ variable value.
pyr += '#'
//Prints the array to the console
console.log(pyr)
}
}
// calls the function
makeDownwardStairs(5);
Related
I have a problem in JavaScript programing. I am beginner and from last 24 hrs I just completed one task that made pyramid star pattern which only work in console but the problem is this, that the same coding doesn't work on browser. In the browser the triangle become 📐 such type of triangle.
I added br to code for the browser but the triangle become 📐 right angle triangle.
use pre tag to render your result, so it doesn't remove extra spaces. most html tag will remove extra spaces because it thinks those are useless.
let starCount = 1;
const row = 35
let result = "";
for (let i = 0; i < row; i++) {
let starString = "";
const p = starCount;
for (let j = 0; j < row; j++) {
if (j >= ((row - p) / 2) && j < (((row - p) / 2) + p)) {
starString += "*";
continue;
}
starString += " "
}
result += starString + "\n";
if (row % 2 == 0) {
if (i < (row / 2) - 1) {
starCount += 2;
}
if (i > ((row / 2) - 1)) {
starCount -= 2;
}
} else {
if (i < Math.floor(row / 2)) {
starCount += 2;
} else {
starCount -= 2;
}
}
}
document.querySelector("pre").innerHTML = result;
<pre></pre>
I am trying to compare two sentences ("Kat" and input "Spat") using Levenshtein Distance. If the sentences are similar enough, I want a "correct" text to appear. The script, which I have copied, works fine, but I am having troubles with the If-statement. I want a "correct" text to appear, if the Levenshtein distance is measured to "2" (As is the case with "Kat" and "Spat"), but I don't know which variable should be set as equal to "2".
//Codes by Jared Stilwell
var matrix;
var first = "Kat";
function levenshteinDistance(a, b) {
if (b.length == 0) return a.length;
matrix = [];
// increment along the first column of each row
for (var i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
// increment each column in the first row
for (var j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// fill in the rest of the matrix
for (i = 1; i <= b.length; i++) {
for (j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // substitution
Math.min(
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1
)
); // deletion
}
}
}
return matrix[b.length][a.length];
}
function updateDistance() {
var second = $(".term-2").val();
$(".js-distance").text(levenshteinDistance(first, second));
}
$(".term-2").on("keyup", function(ev) {
ev.preventDefault();
updateDistance();
});
updateDistance();
//My own part which I can't figure out
function testknap() {
if (matrix == 2) // I CAN'T FIGURE OUT THIS PART?
document.getElementById("correct_text_here").innerHTML = "Correct";
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="js-distance">0</div>
<!-- The distance points -->
<!-- Textform -->
<input class="term-2" value="Spat" placeholder="Skriv her">
<button onclick="testknap()">Tryk</button>
<!-- A submit button -->
<!-- Text which appear if the input is correct -->
<p id="correct_text_here"></p>
Just read the value
function testknap() {
if (document.querySelector(".js-distance").textContent == 2)
document.getElementById("correct_text_here").innerHTML = "Correct";
};
//Codes by Jared Stilwell
var matrix;
var first = "Kat";
function levenshteinDistance(a, b) {
if (b.length == 0) return a.length;
matrix = [];
// increment along the first column of each row
for (var i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
// increment each column in the first row
for (var j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// fill in the rest of the matrix
for (i = 1; i <= b.length; i++) {
for (j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // substitution
Math.min(
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1
)
); // deletion
}
}
}
return matrix[b.length][a.length];
}
function updateDistance() {
var second = $(".term-2").val();
$(".js-distance").text(levenshteinDistance(first, second));
}
$(".term-2").on("keyup", function(ev) {
ev.preventDefault();
updateDistance();
});
updateDistance();
function testknap() {
if (document.querySelector(".js-distance").textContent == 2)
document.getElementById("correct_text_here").innerHTML = "Correct";
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="js-distance">0</div>
<!-- The distance points -->
<!-- Textform -->
<input class="term-2" value="Spat" placeholder="Skriv her">
<button onclick="testknap()">Tryk</button>
<!-- A submit button -->
<!-- Text which appear if the input is correct -->
<p id="correct_text_here"></p>
I am currently trying to solve the xmas tree problem, with internal tree-like shape.
issue is with internal spacing, it supposed to be like: 1, 5, 7, 9. Instead it is 1, 3, 4, 5. I do not know, how to increment s loop by 2 in each loop turn.
/*
*********
**** ****
*** ***
** **
* *
*********
*/
function drawTree(h) {
let n = h + 3;
for (var i = 1; i <= 1; i++) {
var temp = "";
for (var j = 1; j <= n; j++) {
temp = temp + "*";
}
console.log(temp);
}
for (var i = 0; i < h - 2; i++) {
var tree = '';
console.log("\n");
for (var k = 3; k <= h - i; k++) {
tree += "*";
};
tree += "s";
for (var k = 1; k <= i; k++) {
for (var k = 1; k <= i; k++) {
tree += "s";
};
tree += "s";
};
for (var k = 3; k <= h - i; k++) {
tree += "*";
};
console.log(tree);
};
console.log("\n");
let g = h + 3;
for (var i = 1; i <= 1; i++) {
var temp = "";
for (var j = 1; j <= g; j++) {
temp = temp + "*";
}
console.log(temp);
}
};
drawTree(6);
function drawTree(stars, rowLength) {
for (let row = 0; row < rowLength; row++) {
if (row === 0) {
console.log("*".repeat(stars));
} else if(row === rowLength - 1) {
console.log("*".repeat(stars));
} else {
let spaces = 2 * row - 1;
if (spaces > stars) {
spaces = stars;
}
let numStarsInRow = "*".repeat((stars - spaces) / 2);
console.log(numStarsInRow + " ".repeat(spaces) + numStarsInRow);
}
}
}
drawTree(9, 5)
You can implement this by nesting loops over the height and the width of the tree, noting that the output is a * whenever:
it's the first or last row; or
the current x position is less than or equal to the halfway point minus the row number; or
the current x position is greater than or equal to the halfway point plus the row number
For all other cases the output is a space. For example:
function drawTree(height) {
// compute the width of the tree from the height
let width = height % 2 ? height + 2 : height + 3;
// find the halfway point
let half = (width - 1) / 2;
for (let i = 0; i < height; i++) {
let l = '';
for (let j = 0; j < width; j++) {
if (i == 0 || // first row
i == height - 1 || // last row
j <= (half - i) || // left side
j >= (half + i) // right side
) {
l += '*';
}
else {
l += ' ';
}
}
console.log(l);
}
}
drawTree(6);
drawTree(5);
Hello everyone i have a problem with my code and i'm not sure how to do this , i need to write code that draw this in console:
Draw '*' in every even number
For that i need to use nested loops.
So far i have only this:
var n = 5;
var stars = '';
for (var i = 1; i <= n; i++) {
var starsline = '';
for (var j = 1; j <= n; j++) {
console.log(i + j);
}
if ( i % 2 === 0){
starsline += '2';
} else {
starsline += '1'
}
stars += starsline;
}
console.log(stars);
This numbers 2 and 1 are only to check if the number is even or odd.
Just a few things:
1) you got some weird bracket here:
/*}*/ if ( i % 2 === 0){
which causes a syntax error later on.
2) you actually log the right thing:
console.log(i + j)
but you dont use it. Just put that into your condition:
if((i + j) % 2 === 0)
and you are done :)
let size = 5, stars = "";
for (var row = 1; row <= size; row++) {
var starsline = "";
for (var col = 1; col <= size; col++){
if ((row + col) % 2 === 0){
starsline += '*';
} else {
starsline += ' ';
}
stars += starsline + "\n";
}
console.log(stars);
I think what you tried to do is something like this:
var n = 5;
var stars = '';
for (var i = 1; i <= n; i++) {
var starsline = '';
for (var j = 1; j <= n; j++){
if ( (i + j) % 2 === 0){
// used three spaces for consistency in the drawing
starsline += ' ';
} else {
starsline += ' * '
}
}
stars += starsline + '\n';
}
console.log(stars);
Try this:
var n = 5;
var stars = '';
for (var i = 1; i <= n; i++)
{
var starsline = '';//<-- reset the value of line
if ( i % 2 === 0)//<--this identifies which line will the stars be created
{
starsline += '* * *';//<--creating the stars on each line
}
else
{
starsline += ' * * ';//<--creating the stars on each line
}
stars += starsline+'\n';//<-- '\n' add line breaks for each lines
}
console.log(stars);//<-- print the stars
I was trying to find the Rank of the given string in the list of permutations and was hoping someone can find the bug.
function permute() {
var W = $('input').val(),
C = [];
for (var i = 0; i < 26; i++) C[i] = 0;
var rank = 1;
for (var i = 0; i < W.length; i++) {
C[W.charCodeAt(i) - 'a'.charCodeAt(0)]++;
}
var repeated= 1;
for (var i = 0; i < C.length; i++) {
if(C[i] > 0) {
repeated *= fact(C[i]);
}
}
if (W !== '') {
for (var i = 0; i < W.length; i++) {
//How many characters which are not used, that come before current character
var count = 0;
for (var j = 0; j < 26; j++) {
if (j == (W.charCodeAt(i) - 'a'.charCodeAt(0))) break;
if (C[j] > 0) count++;
}
C[W.charCodeAt(i) - 'a'.charCodeAt(0)] = 0;
rank += ( count * fact(W.length - i - 1) );
}
rank = rank/ repeated;
}
var pp = 'Rank of :: ' + W + ' -- ' + rank;
$('div').append('<p>' + pp + '</p>');
}
function fact(n) {
if (n == 0 || n == 1) return 1;
else return fact(n - 1) * n;
}
$('button').click(permute);
Check Fiddle
A use case for this might be
bookkeeper is supposed to give a rank of 10743.
Here's the demo:
For each position check how many characters left have duplicates, and use the logic that if you need to permute n things and if 'a' things are similar the number of permutations is n!/a!