Hi,
I never expected to solve these three loop-assignents within 24h.
But I found it highly motivating to get a reply from a real person and I would like to thank you in this post as well as asking for assessment for number three which is free for all to reply of course. So thank you @justsomeone!
I added two additional parts of my if and else if statements to get a nice ending on the text and not just the same separator all the way.
you doing great as usual well done and i do not deserve those nice words
but since you asked me to assets this one then it will cost you 2 chocolate bar just kidding i am glad to help any time
another thing but i need @mikoMK opinion about it
is there performance hit by calling para.textContent many time in other word
would it be better to do the concatenation to variable then finally assign it to para.textContent
so the code become like that
while (i > 1) {
if (i === 2) {
myPrimeNumbers += ` and ${i}.`;
} else if (i === 3) {
myPrimeNumbers += `${i} `;
} else if (isPrime(i)) {
myPrimeNumbers += `${i}, `;
}
i--;
}
para.textContent = myPrimeNumbers;
i tried that code to make sure that para is kept appended and the browser update it for each append
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function x(){
while (i > 1) {
await sleep(2000);
if (i === 2) {
para.textContent += ` and ${i}.`;
} else if (i === 3) {
para.textContent += `${i} `;
} else if (isPrime(i)) {
para.textContent += `${i}, `;
}
i--;
}
}
x();
do not worry about the extra code i used @Ekeblad you will learn about it later i just wanted @mikoMK opinion if my guess was right or i miss something else
@Ekeblad: I’m glad you’re enjoying the MDN exercises and the community.
@justsomeone: I think it doesn’t really matter since the DOM gets only manipulated once in both versions. This happens with section.appendChild(para); at the end. Before para is just an HTMLElement in JS without any connection.
It would be different if we would removeChild() the old para and appendChild() the new para in every iteration of the loop. This should be avoided.