Skip to content
Advertisement

While printing my function, the string is printing out a double set of commas?

The assignment is to create our own snap-crackle game from a function.

I believe I did step one of this correctly, now I’m on step two, and it’s printing what I want it to print, but the string has double commas, between the elements.

Could someone assists me and point me in the right direction with this?

I’m not exactly sure what did I’m doing wrong in this case.

For background purposes, I will share the instructions and my code so far. Thank you to anyone for the help.

  • Step One

    Write a function named snapCrackle that takes one parameter: maxValue. This function should loop through 1 up to maxValue (inclusive) and build a string with the following conditions:

  • If a number is odd, concatenate “Snap, ” to the end of the string.

  • If a number is a multiple of 5, concatenate “Crackle, ” to the end of the string.

  • If a number is both odd and a multiple of 5 concatenate “SnapCrackle, ” to the end of the string.

  • If a number is neither odd nor a multiple of 5, concatenate the number and “, ” to the end of the string.

    // Your Code Here.
    let maxValue = 12;
    let newArray = [];
    let s = "Snap,";
    let c = "Crackle,";
    let x = "Snapcrackle,";
    let n = ",";

    function snapCrackle() {
      for (let i = 1; i <= maxValue; i++) {
        if (i % 2 !== 0 && i % 5 === 0) {
          newArray.push(x);
        } else if (i % 2 !== 0) {
          newArray.push(s);
        } else if (i % 5 === 0) {
          newArray.push(c);
        } else {
          newArray.push(i + n);
        }
      }
      return newArray;
    }

    console.log(snapCrackle());


  1. Step Two

    Write a function called render that takes in two parameters: text, and maxValue. This function should print the game to the page using document.write().

let text = newArray;
function render (text, maxValue){
  document.write(`
  <h1>Snap Crackle!</h1>
  <h3>Max Value ${maxValue}</h3>
  <p>${text}</p>
  `);
}
console.log(render(text,maxValue));

Full Code:

// Your Code Here.
let maxValue = 12;
let newArray = [];
let s = "Snap,";
let c = "Crackle,";
let x = "Snapcrackle,";
let n = ",";

function snapCrackle() {
  for (let i = 1; i <= maxValue; i++) {
    if (i % 2 !== 0 && i % 5 === 0) {
      newArray.push(x);
    } else if (i % 2 !== 0) {
      newArray.push(s);
    } else if (i % 5 === 0) {
      newArray.push(c);
    } else {
      newArray.push(i + n);
    }
  }
  return newArray;
}



let text = snapCrackle();


function render(text, maxValue) {
  document.write(`
  <h1>Snap Crackle!</h1>
  <h3>Max Value ${maxValue}</h3>
  <p>${text}</p>
  `);
}
render(text, maxValue)

Advertisement

Answer

You included commas in the strings you created, which you didn’t need to do. You can just remove the commas from the strings. Javascript automatically includes commas when printing arrays.

// Your Code Here.
let maxValue = 12;
let newArray = [];
let s = "Snap";
let c = "Crackle";
let x = "Snapcrackle";
// var n is unrequired now

function snapCrackle() {
  for (let i = 1; i <= maxValue; i++) {
    if (i % 2 !== 0 && i % 5 === 0) {
      newArray.push(x);
    } else if (i % 2 !== 0) {
      newArray.push(s);
    } else if (i % 5 === 0) {
      newArray.push(c);
    } else {
      newArray.push(i); // removed + n
    }
  }
  return newArray;
}



let text = snapCrackle();


function render(text, maxValue) {
  document.write(`
  <h1>Snap Crackle!</h1>
  <h3>Max Value ${maxValue}</h3>
  <p>${text}</p>
  `);
}
render(text, maxValue)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement