0

I am very new to programming, so this might be a simple solution that I simply don't know. I have been trying to write a code that takes a specific number, for example "10", and returns a pyramid where the base has the number of asterisks matched by the value you enter. We are supposed to use this in our code:

function makeLine(length) {
  var line = "";
  for (var j = 1; j <= length; j++) {
    line += "* "
  }
  return line + "\n";
}

For example:

image of triangle where the base is matched by the number entered by the programmer

My code so far looks like this. I know it's completely wrong because I am not at all getting the required results. What am I doing wrong??

function makeLine(length) {
    var line = "";
    for (var j = 1; j <= length; j++) {
        line += "* ";
    }
    return line + "\n";
    function makeTriangle(length) {
        var line = "";
        for (var j = 1; j <= length; j--) {
            line += "* ";
        }
    } 
} 
console.log(makeTriangle(8));
2
  • 2
    I think that, you're supposed to use the method makeLine that was given to you, meaning that anything you add to your program shoud not modify it. It seems you added some code to it, which is probably not what was expected. Try using the function for what it is, a "function", or a tool, that can be called to serve its purpose from other parts of the program. This function has one and only one responsibility which is to return a string representing a line, try to think of the general algorithm that would make non intrusive use of this responsibility to reach the final objective. Commented Jul 26, 2017 at 20:02
  • think of the steps you would go through to do this. a general rule of thumb is: if you can work it out on paper, then you can turn the steps you used to work it out on paper into code that does it automatically. Commented Jul 26, 2017 at 20:06

4 Answers 4

0

You need two for loops: one to keep track of the length, and another one to keep track of the current line you are on.

function makeLine(length) {
  var line = "";
  for (var i = 1; i <= length; i++) {
    for (var j = 1; j <= i; j++) {
      line += "* "
    }
    line += "\n";
  }
  return line
}
Sign up to request clarification or add additional context in comments.

Comments

0

Example implementation if you are on es2015.

function makeLine(length) {
  return Array(length).fill('*').join('');
}

function makePyramid(height) {
  const result = [];
  for (let i = 0; i < height; i + 1) {
    result.push(`${makeLine(i + 1)}\n`);
  }
  return result.join('');
}

//*
//**
//***
//****
console.log(makePyramid(4));

Comments

0

I had this question on a course I was taking and solved it like this:

    function makeLine(length) {
    var line = "";
    for (var j = 1; j <= length; j++) {
        line += "* ";
    }
    return line + "\n";
}

    function buildTriangle(height) {
    var triangle = "";
    for (var i = 1; i <= height; i++) {
        // call makeline function
        triangle += makeLine(i);
    }
    return triangle;
}

    console.log(buildTriangle(10));

Comments

0

let newElem = document.createElement("span");

for (let i = 0; i <= string.length; i++) {

for (let j = 0; j < i; j++) {

    newElem[j] = document.createElement("span");
    newElem[j].setAttribute("class", "display");enter code here
    newElem[j].innerHTML = string[j]
    document.body.appendChild(newElem[j])
    console.log(string[j]);
}
    document.write("<br>")    

}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.