DEV Community

nnguyen52
nnguyen52

Posted on

Seek-Help!

hi everyone and have a nice day!

im pretty new to javascript as well as propramming. (been for only 1 month)

im practicing this question (im NOT allowed to use if-then operator as teacher's assignment):

Here is a sample:
first: Q. I made 26 cupcakes and ate 15 of them. How many am I left with?
A. 11

second: The next time the page loads, the numbers should change, so the problem may look like:
Q. I made 45 cupcakes and ate 2 of them. How many am I left with?
A. 43

third: The numbers used in this word problem should change randomly. This means
that we need to generate the numbers for the questions and the answer using
JavaScript.

HINT: you will need to use the Math.random() method to complete this part.
Here are the characteristics of the problem along with the possibilities you need to consider:

  • The generated random numbers should always be between 0 and 50.
  • You should never encounter a scenario in which you make less cupcakes and eat more. For example: Q: I made 45 cupcakes and ate 47 of them. How many am I left with?
  • In case you don’t eat any cupcakes, the question should say: Q: I made 45 cupcakes and ate none of them. How many am I left with? instead of saying Q: I made 45 cupcakes and ate 0 of them. How many am I left with? and the answer should say: A. I don't need to do any calculations.

+ You are not allowed to use the if…then construct.

This is my code (i still have errors and issues):

var made= Math.floor(Math.random() * 50) + 1;
var eat = Math.floor(Math.random() * 50) + 0;
let b= (eat== 0? "none" : eat);
console.log ("Q. I made " + (made== 1? "1 cupcake": made + " cupcakes") + " and ate " + b + " of them. How many am i left with?");
console.log ("A: " + (b== "none"?"I don't need to do any calculations.": (0 <= eat < made <= 50, made-eat)));
document.write ("Q. I made " + (made== 1? "1 cupcake": made + " cupcakes") + " and ate " + b + " of them. How many am i left with?" + "<br>");
document.write ("A: " +(b== "none"? "I don't need to do any calculations.": (0 <= eat < made <= 50, made-eat)) );

Top comments (6)

Collapse
 
arrlancore profile image
arrlancores • Edited

Hi, maybe I can help, you can check the live version here codesandbox

function randomNumber() {
  const generateRandom = () => Math.floor(Math.random() * 50)
  const number = generateRandom();
  return number > 10 ? number : number + 10
}

function getLowerNumber (num) {
  let getGreaterNumber = randomNumber()
  while(getGreaterNumber >= num && getGreaterNumber !== 0) {
    getGreaterNumber = randomNumber()
  }
  return getGreaterNumber
}

const cakeMade = randomNumber()
const cakeEat = getLowerNumber(cakeMade)

document.getElementById("app").innerHTML = `
<h1>Q: I made ${cakeMade} cupcakes and ate ${cakeEat ? cakeEat : 'none'} of them. How many am i left with?</h1>
<h1>A: ${cakeEat === 0 ? 'I don`t need to do any calculations.' : cakeMade - cakeEat}</h1>
`;
Collapse
 
avalander profile image
Avalander • Edited

Using a while loop to generate a number of eaten cupcakes smaller than the amount made is unnecessary, it's better to generate the amount of cupcakes and then use that number as the ceiling for the amount of eaten cupcakes.

function randInt(start, end) {
  return Math.floor(Math.random() * (end - start)) + start
}

const made = randInt(0, 50)
const eaten = randInt(0, made)
Collapse
 
arrlancore profile image
arrlancores • Edited

Yes it's make sense to use start-end range for eaten and or made cakes and should be better. Already updated my code on sandbox

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
nnguyen52 profile image
nnguyen52

Hi Sir, i checked the code you guided me, it seems not working somehow. and im just getting to While-operation and i have not known about $, then i got confused. can you make it simplified ?

Thread Thread
 
arrlancore profile image
arrlancores

It's "template literal string in javascript" if you want to look at it. But you can simply change with + for join string like this:

"Q: I made " + cakeMade + " cupcakes "

Some comments may only be visible to logged-in visitors. Sign in to view all comments.