0

I am currently trying to complete an assignment for an intro2Javascript course. The question basically asks me to return a string of multiples of 2 parameters (num, numMultiple). Each time it increments the value i until i = numMultiple. For example:

5 x 1 = 5\n 5 x 2 = 10\n 5 x 3 = 15\n 5 x 4 = 20\n

This was my attempt:

function showMultiples(num, numMultiples) {
  var result;
  for (i = 1; i <= numMultiples; i++) {
    result = num * i
    multiples = "" + num + " x " + i + " = " + result + "\n"
    return (multiples)
  }
}

...and because the assignment comes with pre-written console logs:

console.log('showMultiples(2,8) returns: ' + showMultiples(2, 8));
console.log('showMultiples(3,2) returns: ' + showMultiples(3, 2));
console.log('showMultiples(5,4) returns: ' + showMultiples(5, 4));
console.log('\n');

This is my output:

showMultiples(2,8) returns: 2 x 1 = 2
Scratchpad/1:59:1
showMultiples(3,2) returns: 3 x 1 = 3
Scratchpad/1:60:1
showMultiples(5,4) returns: 5 x 1 = 5

UPDATE

enter image description here

0

3 Answers 3

2

You were doing two things incorrectly:

1) You were returning after the first iteration through your loop

2) You were assigning to multiples instead of appending to it.

Since you want to gather all the values and then show the final result first, I add all of the values to an array, and then use unshift() to add the final element (the result) to the beginning of the array. Then I use join() to return a string representation of the desired array.

function showMultiples(num, numMultiples) {
  var result;
  var multiples = [];
  for (let i = 1; i <= numMultiples; i++) {
    result = num * i
    multiples.push("" + num + " x " + i + " = " + result + "\n")
  }
  multiples.unshift(multiples[multiples.length-1]);
  return (multiples.join(''))
}

console.log('showMultiples(2,8) returns: ' + showMultiples(2, 8));
console.log('showMultiples(3,2) returns: ' + showMultiples(3, 2));
console.log('showMultiples(5,4) returns: ' + showMultiples(5, 4));
console.log('\n');

Sign up to request clarification or add additional context in comments.

Comments

1

You need to declare all variables, because without you get global variables (beside that it does not work in 'strict mode').

The second point is to use multiples with an empty string for collecting all intermediate results and return that value at the end of the function.

For keeping the last result, you could use another variable and append that value at the end for return.

function showMultiples(num, numMultiples) {
    var i,
        result,
        multiples = "",
        temp = '';

    for (i = 1; i <= numMultiples; i++) {
        result = num * i;
        temp = num + " x " + i + " = " + result + "\n";
        multiples += temp;
    }

    return temp + multiples;
}

console.log('showMultiples(2,8) returns: ' + showMultiples(2, 8));
console.log('showMultiples(3,2) returns: ' + showMultiples(3, 2));
console.log('showMultiples(5,4) returns: ' + showMultiples(5, 4));

Comments

0

As other answers say, your problem is in multiple.

You are clearing multiple every iteration and storing the new value, but you do not want that, you want to add the new result, and to do so you use this code:

multiples = multiple + "" + num + " x " + i + " = " + result + "\n"

which can be compressed in what the rest of the people answered:

multiples += "" + num + " x " + i + " = " + result + "\n"

Probably you already know, but to ensure:

a += b ---> a = a + b
a -= b ---> a = a - b
a *= b ---> a = a * b

and there are even more.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.