Comments (12)
while learning JS, i ran into a small issue and im stuck . Any help rendered would be appreciated
Hi ,In the for loop ,both initial and final value of i is inclusive:
for(let i=5;i<=10;i++)
due to which **loop runs 6 times ** i think that might be the issue .Hope it helps.
from javascript.
Hello, it still doesn't give me the required answer
Thanks for the help though
from javascript.
try running **i from 0 to n inside loop ** . I think it might work.
from javascript.
still didnt work for me
from javascript.
Hi @dree-max, there's a super easy approach without loops if you want to... i.e. using .repeat()
.
You can simply solve this in 4 lines of code:
function scream(n) {
let str = 'a'.repeat(n)
console.log(str)
}
scream(10)
Lemme know if you have any questions. Cheers :)
from javascript.
Unfortunately this one has to have loops because it's what's been examined in this particular exercise!
Thank you for the other option though I tried it out and it works just fine
from javascript.
You can do it like this!
function scream(n)
{
let str = '';
while(n--)
{
str +='a';
}
console.log(str);
}
scream(10);
from javascript.
Hello @ dree-max, you have already figured that out but still useful to give my two cents.
In Pseudocode, that problem would be solved like this:
n = 5
i = 0
from i to n:
// do
In JavaScript, that would be solved like this:
function scream(n) {
let str = "";
for (let i = 0; i<n; i++)
str = str + 'a';
return str;
}
console.log(scream(2)); // aa
console.log(scream(5)); // aaaaa
from javascript.
@dree-max run loop as i=0 ; i < n; i++
this have to work
from javascript.
function scream(n) {
let str = "";
for (let i = 0; i<n; i++)
str = str + 'a';
return str;
}
console.log(scream(3)); // aaa
console.log(scream(5)); // aaaaa
//This will solve the issue.
Happy to help :)
from javascript.
function scream(n) {
return Array(n+1).join('a');
}
console.log(scream(3)); // aaa
console.log(scream(5)); // aaaaa
Hey @dree-max , Easy approach without loops if you want to..(using an Array and then join it with 'a' as the separator.)
from javascript.
// simple solution with for loop
`function scream(n) {
let str = '';
for (let i = 0; i < n; ++i) {
str += 'a';
}
return str;
}
console.log(scream(5));
console.log(scream(10));`
from javascript.
Related Issues (20)
- Implement ES6 features HOT 2
- Trial Duration In JavaScript
- Java
- Javascript
- Study
- LetsUpgrade JavaScript Batch 2 Assignment Day 2
- Asynchronous programming and handling async operations with promises HOT 1
- problems
- text
- None of the maintainers are looking at the pull request made HOT 1
- Want to contribute
- Average
- JSL
- Average
- Js Loops
- [feature-Request] Dark mode make website more attractive HOT 2
- GTA
- My exercices
- JavaFX
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.